Wednesday 20 August 2014

Here is the list of common T-codes used in ABAP module.

sap-transactions-list-dictionary

Monday 18 August 2014

Applying SORTING in Object Oriented ALV ( OO ALV )

Sorting in ALV always means that sort  settings applied for exactly one column , if someone says that
 an ALV display is sorted by 2 fields will simply means that the particular columns is sorted two times ., which is referres to as sort criteria . Sorting is very important functionality while reporting . Similar to filtering and aggregations , the user can do sorting  programmatically as well as using the ALV
 application toolbar functionality .

Similar to all other ALV applications , in order to create sorting you need to follow only 2 steps :
Step 1 : Get the object reference of class CL_SALV_SORTS by calling the method get_sorts( ) of classCL_SALV_TABLE.
lo_sorts = lo_alv->get_sorts( ).

STEP 2 : Call the add_sort( ) method of class CL_SALV_SORTS and passing the column name that
 you want to sort as the parameter in our case it is BELNR (Document Number).
CALL METHOD lo_sorts->add_sort
  
EXPORTING
    columnname = 
'BELNR'.

Please refer to the following source codes :

*******************************************************************
*& Author      : Roushan Kumar
*& Data        : 13/06/2014
*& Description : Sorting in OO ALV
*******************************************************************
REPORT  ztestr_alv_8.
TYPES : BEGIN OF gy_bseg,
          bukrs  
TYPE bukrs,
          belnr  
TYPE belnr_d,
          gjahr  
TYPE gjahr,
          buzei  
TYPE buzei,
          wrbtr 
TYPE wrbtr,
        
END OF gy_bseg.
DATA : gt_bseg TYPE STANDARD TABLE OF gy_bseg INITIAL SIZE 1.
REFRESH : gt_bseg[].
SELECT bukrs belnr gjahr buzei wrbtr
       
FROM bseg
       
INTO TABLE gt_bseg
       
UP TO 10 ROWS.IF sy-subrc IS INITIAL.

  
SORT gt_bseg BY bukrs belnr gjahr buzei .
ENDIF.
DATA : lo_alv TYPE REF TO cl_salv_table.
TRY.
    
CALL METHOD cl_salv_table=>factory
      
IMPORTING
        r_salv_table = lo_alv
      
CHANGING
        t_table      = gt_bseg.
  
CATCH cx_salv_msg .ENDTRY.
*-- Sorting logic starts
DATA : lo_sorts TYPE REF TO cl_salv_sorts.
*-- Step 1 : get the cl_salv_sorts object ref.
lo_sorts = lo_alv->get_sorts( ).
**-- Step 2 : get sorting for column belnr
CALL METHOD lo_sorts->add_sort
  
EXPORTING
    columnname = 
'BELNR'.
*-- Sorting logic end

lo_alv->display( ).
 

OUTPUT :
Sorting in OO ALV
Sorting in OO ALV























You can also change the individual sortings for a particular column by just following the two steps similar 
to above :
Step 1 : Get the object reference of class CL_SALV_SORT by calling the method get_sort( ) of classCL_SALV_SORTS and passing the column name as parameter .
lo_sort = lo_sorts->get_sort(
                             columnname = 
'BELNR'
                            ).
STEP 2 : Call the set_group ( ) method of class CL_SALV_SORT and passing the parameter .
lo_sort->set_group(
                   
value = if_salv_c_sort=>group_with_underline
                  ).
The  value of interface constants that will pass as the parameter value of method set_group( ) with there
 attributes are as follows :
Interface Constant
Attributes
IF_SALV_C_SORT=>GROUP_WITH_UNDER-LINE
Underlined
IF_SALV_C_SORT=>GROUP_WITH_NEW-PAGE
Page change
IF_SALV_C_SORT=>GROUP_NONE
No Display

Please refer to the below source codes :
*******************************************************************
*& Author      : Roushan Kumar
*& Data        : 13/06/2014
*& Description : Sorting in OO ALV
*******************************************************************
REPORT  ztestr_alv_8.
TYPES : BEGIN OF gy_bseg,
          bukrs  
TYPE bukrs,
          belnr  
TYPE belnr_d,
          gjahr  
TYPE gjahr,
          buzei  
TYPE buzei,
          wrbtr 
TYPE wrbtr,
        
END OF gy_bseg.
DATA : gt_bseg TYPE STANDARD TABLE OF gy_bseg INITIAL SIZE 1.
REFRESH : gt_bseg[].
SELECT bukrs belnr gjahr buzei wrbtr
       
FROM bseg
       
INTO TABLE gt_bseg
       
UP TO 10 ROWS.IF sy-subrc IS INITIAL.

  
SORT gt_bseg BY bukrs belnr gjahr buzei .
ENDIF.
DATA : lo_alv TYPE REF TO cl_salv_table.
TRY.
    
CALL METHOD cl_salv_table=>factory
      
IMPORTING
        r_salv_table = lo_alv
      
CHANGING
        t_table      = gt_bseg.
  
CATCH cx_salv_msg .ENDTRY.
*-- Sorting logic starts
DATA : lo_sorts TYPE REF TO cl_salv_sorts.
*-- Step 1 : get the cl_salv_sorts object ref.
lo_sorts = lo_alv->get_sorts( ).
**-- Step 2 : get sorting for column belnr
CALL METHOD lo_sorts->add_sort
  
EXPORTING
    columnname = 
'BELNR'
    
position   = 1
    
group      = if_salv_c_sort=>group_with_newpage.
DATA : lo_sort TYPE REF TO cl_salv_sort.

lo_sort = lo_sorts->get_sort(
                             columnname = 
'BELNR'
                            ).

lo_sort->set_group(
                   
value = if_salv_c_sort=>group_none
                  ).
*-- Sorting logic end

lo_alv->display( ).

OUTPUT :

Sorting in OO ALV
Sorting in OO ALV

CHANGE TEXT ALIGNMENT OF COLUMN IN Object Oriented ALV ( OO ALV )

In this SAP ABAP tutorial I will show you how to  change the alignment of text of particular column or columns in Object Oriented ALV ( OO ALV ) .
Programmatically now you can change the text alignment on three possibilities :
1.Left aligned.
2.Centered.
3.Right aligned.
N.B : By default numericals values are right aligned and words are left aligned.Now , we can change this
 according to our own taste.
In order to do this we need to call the SET_ALIGNMENT method of class CL_SALV_COLUMN .
Please refer to the following source codes :  
*******************************************************************
*& Author      : Roushan Kumar
*& Data        : 15/06/2014
*& Description : Change text alignment in OO ALV
*******************************************************************
REPORT  ztestr_alv_14.
TYPES : BEGIN OF gy_vbap,
          vbeln 
TYPE vbeln_va,
          posnr 
TYPE posnr_va,
          matnr 
TYPE matnr,
        
END OF   gy_vbap.
DATA : gt_vbap TYPE STANDARD TABLE OF gy_vbap INITIAL SIZE 1.
DATA : lo_alv TYPE REF TO cl_salv_table.REFRESH : gt_vbap[].

START-
OF-SELECTION.

  
SELECT vbeln posnr matnr
         
FROM vbap
         
INTO TABLE gt_vbap
         
UP TO 10 ROWS.
  
IF sy-subrc IS INITIAL.

    
SORT gt_vbap BY vbeln posnr matnr  .

  
ENDIF.

  
TRY.
      
CALL METHOD cl_salv_table=>factory
        
IMPORTING
          r_salv_table = lo_alv
        
CHANGING
          t_table      = gt_vbap.
    
CATCH cx_salv_msg .
  
ENDTRY.
*-- Change text alignment logic starts

  
DATA : lo_columns TYPE REF TO cl_salv_columns_table.
  lo_columns = lo_alv->get_columns( ).
  lo_columns->set_optimize( 
'X' ).

  
DATA : lo_column TYPE REF TO cl_salv_column_table.
  lo_column ?= lo_columns->get_column( 
'POSNR' ).

  
CALL METHOD lo_column->set_alignment
    
EXPORTING
      
value = if_salv_c_alignment=>left.
*-- Change text alignment logic end

  lo_alv->display( ).

OUTPUT :   


CHANGE TEXT ALIGNMENT OF COLUMN IN Object Oriented ALV ( OO ALV )
CHANGE TEXT ALIGNMENT OF COLUMN IN Object Oriented ALV ( OO ALV )