Dynamic subtotal in smartform

By Jayshree Santosh Mahajan, Cognizant

Scenario:

Sort sequences will be maintained in the table. Subtotal of the amount field should be performed based on the first field of the sort sequence.

Below is the sample code to achieve this.

In this example sort sequences are maintained in the table ZVBRK_SORT.

The contents of the table ZVBRK_SORT are

We will get the tables contents run time that is nothing but dynamic sort sequence.

Steps:

1.  Get the sort sequences and sort the target internal table.

2.  Get the attributes of the first field of the sort sequences.

3.  Create dynamic internal table of the first field and the amount field.

4.  Get data from the target internal table to the dynamic internal table.

5.  Perform subtotal on dynamic internal table using the AT END OF control statement.

6.  Insert the sub totaled line into the target internal table

7.  Pass the internal table to the smart form.

DATA : lt_vbrk_sort TYPE TABLE OF zvbrk_sort,
       ls_vbrk_sort 
TYPE zvbrk_sort,
       lt_sort     
TYPE abap_sortorder_tab,
       ls_sort      
TYPE abap_sortorder,
       lt_subto     
TYPE ZTT_VBRK,
       ls_subto    
TYPE ZLT_VBRK,
       lt_comp      
TYPE cl_abap_structdescr=>component_table,
*      components
       ls_comp      
TYPE abap_componentdescr,
*      components
       ls_components 
TYPE abap_compdescr,
*         Counter
          l_count       
TYPE i,
*         Index
          l_index       
TYPE sy-tabix.

DATA :    l_or_tab_ref  TYPE REF TO cl_abap_tabledescr,
          l_or_ref      
TYPE REF TO cl_abap_structdescr,
*         l_or_strtyp will content new dynamic structure created
          l_or_strtyp    
TYPE REF TO cl_abap_structdescr,
*         l_or_tabtyp will content new dynamic table created
          l_or_tabtyp    
TYPE REF TO cl_abap_tabledescr,
*         l_or_data will be our point to reference data
          l_or_t_data   
TYPE REF TO data,
          l_or_s_data   
TYPE REF TO data.

CONSTANTS : lc_netwr TYPE char5 VALUE 'NETWR'.  "Amount field

*                  Field Symbols to handle Table
    FIELD-SYMBOLS: <lt_data> 
TYPE STANDARD TABLE,
                   <ls_data> 
TYPE ANY,
                   <l_field> 
TYPE ANY,
                   <l_netwr> 
TYPE ANY.
*   Initialize counter to 1
    l_count = 
1.

SELECT *
  
INTO TABLE lt_vbrk_sort
  
FROM zvbrk_sort.

  
If SY-SUBRC = 0.
    
SORT lt_vbrk_sort BY zindex.
*     Populate sort internal table
      
LOOP AT lt_vbrk_sort INTO ls_vbrk_sort.
        ls_sort-name = ls_vbrk_sort-fname.
        
APPEND ls_sort TO lt_sort.
        
CLEAR ls_sort.
      ENDLOOP.
*   Sort the internal table
    
SORT it_vbrk  BY (lt_sort).
  ENDIF.
*     Get the fieldnames of the it_vbrk table
      l_or_tab_ref ?= cl_abap_typedescr=>describe_by_data( it_vbrk ).
      l_or_ref ?= l_or_tab_ref->get_table_line_type( ).
*  Perfrom subtotal on first field of the sort table
  
READ TABLE lt_sort INTO ls_sort INDEX 1.
  
IF sy-subrc = 0.
*     Get the sort field attributes
      
READ TABLE l_or_ref->components  INTO ls_components WITH KEY name = ls_sort-name.
      
IF sy-subrc = 0.
        ls_comp-name = ls_sort-name.
        ls_comp-type = cl_abap_elemdescr=>get_c( p_length = ls_components-length ).
        
APPEND ls_comp TO lt_comp.
        CLEAR: ls_comp.
      ENDIF.
*     Get amount field's attributes

READ TABLE l_or_ref->components                  INTO ls_components WITH KEY name = lc_netwr.
 
IF sy-subrc = 0.
   ls_comp-name = lc_netwr.
   ls_comp-type = cl_abap_elemdescr=>get_c( p_length = ls_components-                length ).
   
APPEND ls_comp TO lt_comp.
   CLEAR: ls_comp.
   l_or_strtyp = cl_abap_structdescr=>create( lt_comp ).
 ENDIF.

 l_or_tabtyp = cl_abap_tabledescr=>create( p_line_type  = l_or_strtyp
                       p_table_kind = cl_abap_tabledescr=>tablekind_std
                                         p_unique     = abap_false ).
*     Data to handle the new table type.
      
CREATE DATA l_or_t_data  TYPE HANDLE l_or_tabtyp.


*     New internal table referenced by field symbol
      
ASSIGN l_or_t_data->* TO <lt_data>.


      
CREATE DATA l_or_s_data LIKE LINE OF <lt_data>.
      
ASSIGN l_or_s_data->* TO <ls_data>.

*     Get data into new careated dynamic internal table
      
CLEAR wa_vbrk.
      
LOOP AT it_vbrk INTO wa_vbrk.
        
MOVE-CORRESPONDING wa_vbrk TO <ls_data>.
        
APPEND <ls_data> TO <lt_data>.
      ENDLOOP.

*    Perform actual subtotal
*     Perform subtotal
      
LOOP AT <lt_data> ASSIGNING <ls_data>.
        
ASSIGN COMPONENT ls_sort-name OF STRUCTURE <ls_data> TO <l_field>.

        
ASSIGN COMPONENT lc_netwr OF STRUCTURE <ls_data> TO <l_netwr>.

        
ADD <l_netwr> TO ls_subto-netwr.

        l_index = sy-tabix + l_count.
        
AT END OF (ls_sort-name).
          
READ TABLE lt_vbrk_sort INTO ls_vbrk_sort INDEX 1.
          
IF SY-SUBRC  = 0.
          
CONCATENATE ls_vbrk_sort-descr <l_field> INTO ls_subto-belnr SEPARATED BY space.
          ENDIF.
          
TRANSLATE ls_subto-belnr TO UPPER CASE.
          
INSERT ls_subto INTO it_vbrk INDEX l_index.
          
CLEAR : ls_subto.

          
ADD TO l_count.
        ENDAT.
      ENDLOOP.
  ENDIF.  

Output:

SAP Virtual/Onsite Trainings

Document Categories:

What's New?

Contribute?

Sample Specs

What's Hot?

Web Dynpro for ABAP Tutorials 


Join the Mailing List
Enter name and email address below:
Name:
Email:
Subscribe  Unsubscribe 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image