ALV By Object Oriented Concept

 

  • ALV stands for ABAP List Viewer or Application List Viewer.
  • We can create ALV grid Using Object Oriented Concepts.

Steps for Creating ALV grid Using Object Oriented Concepts :-

1. Create a Field catalog.

  a)Use SAP function module ( LVC_FIELDCATALOG_MERGE ) to create a field catalog merge.

b) Manually create a Field catalog.

2. Create a Custom Control ( layout element ) on a screen.

3. Create an object of the container class ( CL_GUI_CUSTOM_CONTAINER ).

4. Create an Object of the ALV Grid class ( CL_GUI_ALV_GRID ) and pass the object of container class in the parent parameter.

5. Use Method : Use ALV GRID class ( CL_GUI_ALV_GRID ) method ( SET_TABLE_FOR_FIRST_DISPLAY ) to display the data.


let’s implement with a real life scenario

Requirement :-

  • User will provide a range of input for Sales Document Number ( VBELN ).
  • We need to display details of that Sales Document Number from VBAK and VBAP table.
  • We will display the details in form of ALV using the OOPS Concept.

Solution :-

  • Step 1 :- Create a executable program in ABAP Editor ( SE38 ).



  • Step 2 :- Create a type structure for few fields from VBAK table and for few fields from VBAP table.



  • Step 3 :- Now, we need to create a final type structure which will include fields from both the type structures.



  • Step 4 :- Create internal table and work area for all the above type structures.



  • Step 5 :- Create a select option, for Sales Document number, in which user will provide the input on Selection screen.



  • Step 6 :- In Start of Selection event we will write the select query to fetch data from VBAK table and VBAP table based on user input in Sales Document Number.



  • Step 7 :- fill the data into the internal table of final table.




Using Function Module LVC_FIELDCATALOG_MERGE to create a field catalog.

  • For using this function module, we would have to create a a global Structure from SE11 ( Data Dictionary ).



  • Step 8 :- Call this function module inside your program.



  • Step 9 :- Now, we need to create object for CL_GUI_CUSTOM_CONTAINER and CL_GUI_ALV_GRID.



    • We have pass the the object of container as the parent for child.
  • Step 10 :- call method set_table_for_first_display.



  • Step 11 :- we need to call a screen where we will create the above container.






Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_OOPS_ALV
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_oops_alv.

TYPES : BEGIN OF ty_vbak,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
        END OF ty_vbak.

TYPES : BEGIN OF ty_vbap,
          vbeln TYPE vbeln_va,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_vbap.

TYPES : BEGIN OF ty_final,
          vbeln TYPE vbeln_va,
          erdat TYPE erdat,
          erzet TYPE erzet,
          ernam TYPE ernam,
          vbtyp TYPE vbtypl,
          posnr TYPE posnr_va,
          matnr TYPE matnr,
        END OF ty_final.

DATA : lt_vbak  TYPE TABLE OF ty_vbak,
       ls_vbak  TYPE ty_vbak,
       lt_vbap  TYPE TABLE OF ty_vbap,
       ls_vbap  TYPE ty_vbap,
       lt_final TYPE TABLE OF ty_final,
       ls_final TYPE ty_final.
DATA : lt_fieldcat type LVC_T_FCAT.

DATA : lv_vbeln type vbeln_va.
SELECT-OPTIONS : s_vbeln for lv_vbeln.

START-OF-SELECTION.
select vbeln erdat erzet ernam vbtyp
  from vbak into table lt_vbak
  where vbeln in s_vbeln.
  If lt_vbak is not INITIAL.
    select vbeln posnr matnr
      from vbap
      into table lt_vbap
      FOR ALL ENTRIES IN lt_vbak
      where vbeln = lt_vbak-vbeln.

     Loop at lt_vbap into ls_vbap.
       ls_final-posnr = ls_vbap-posnr.
       ls_final-matnr = ls_vbap-matnr.

       READ TABLE lt_vbak into ls_vbak with key vbeln = ls_vbap-vbeln.
       if sy-subrc eq 0.
         ls_final-vbeln = ls_vbak-vbeln.
         ls_final-erdat = ls_vbak-erdat.
         ls_final-erzet = ls_vbak-erzet.
         ls_final-ernam = ls_vbak-ernam.
         ls_final-vbtyp = ls_vbak-vbtyp.
       endif.

       APPEND ls_final to lt_final.
     ENDLOOP.

     CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
*        I_BUFFER_ACTIVE              =
        I_STRUCTURE_NAME             = 'ZAR_SALES_ORDER_STRUCTURE'
*        I_CLIENT_NEVER_DISPLAY       = 'X'
*        I_BYPASSING_BUFFER           =
*        I_INTERNAL_TABNAME           =
       CHANGING
         ct_fieldcat                  = lt_fieldcat
      EXCEPTIONS
        INCONSISTENT_INTERFACE       = 1
        PROGRAM_ERROR                = 2
        OTHERS                       = 3
               .

DATA(lo_cont) = new CL_GUI_CUSTOM_CONTAINER( CONTAINER_NAME = 'CONT' ).
DATA(lo_grid) = new CL_GUI_ALV_GRID( I_PARENT = lo_cont ).

CALL METHOD lo_grid->set_table_for_first_display

  CHANGING
    it_outtab                     = lt_final
    it_fieldcatalog               = lt_fieldcat
  EXCEPTIONS
    invalid_parameter_combination = 1
    program_error                 = 2
    too_many_lines                = 3
    others                        = 4
        .

call screen '0100'.

endif.

*&------------------------------------------------------------------
*&End of Program
*&-----------------------------------------------------------------

Execute the Code :-



  • Press F8.


Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP