Tuesday, 30 July 2013

Developing a simple OO ABAP ALV Report.


In this blog, we will see the steps to be followed to develop a simple OO ABAP ALV Report.

Consider a scenario wherein you have to display the Sales Document details based on the Sales Document Number.

INPUT:








OUTPUT:







The Abstract structure of the program will be as described below.

1. We must declare all the global variables, which are to be used in the program. In our scenario we will be using the following structure and field symbol.

*----------------------------------------------------------------------*
*       GLOBAL DECLARATIONS                                             *
*----------------------------------------------------------------------*

TYPES: begin of ty_vbap,
       VBELN type vbeln_va,
       POSNR type POSNR_VA,
       MATNR type MATNR,
       MATWA type MATWA,
       end of ty_vbap.

FIELD-SYMBOLS: <fs_vbap> type ty_vbap.

2. Next, we have to define the Selection Screen. Since in our scenario we have to retrieve the values using Sales document number, selection screen will have only one parameter, which is Sales Doc.

*----------------------------------------------------------------------*
*       SELECTION SCREEN                                                     *
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 with frame title text-000 no intervals.
PARAMETER: p_vbeln TYPE vbap-vbeln.
SELECTION-SCREEN END OF BLOCK B1.


3. Now we have to define the class and in class we have define the methods either in Public or Private section based on the requirement.

*----------------------------------------------------------------------*
*       CLASS lcl_zdemo_nas DEFINITION                                 *
*----------------------------------------------------------------------*

CLASS lcl_zdemo_nas DEFINITION.

  Public Section.
     METHODS: DISPLAY_ALV.

  Private Section.
     DATA: t_vbap type table of ty_vbap,
                 t_output_alv type table of ty_vbap,
                 wa_output_alv type ty_vbap.

    DATA: w_grid TYPE REF TO CL_GUI_ALV_GRID,                           "ALV GRID
                w_container TYPE REF TO CL_GUI_CUSTOM_CONTAINER, "Container
                t_fieldcat type LVC_T_FCAT,                      "Field catalog Table
               wa_fieldcat type LVC_S_FCAT.                     "Field catalog Structure

    METHODS: TABLE_DISPLAY,
                        FIELD_CATALOG.

ENDCLASS.                    "lcl_zdemo_nas DEFINITION


4. In the Class implementation we will create the object w_container, which refers to the class CL_GUI_CUSTOM_CONTAINER, and w_grid which refers to the class CL_GUI_ALV_GRID and call the method SET_TABLE_FOR_FIRST_DISPLAY of w_grid object and pass the field catalog and the final output table.

 *----------------------------------------------------------------------*
*       CLASS lcl_zdemo_nas IMPLEMENTATION                   *
*----------------------------------------------------------------------*

CLASS lcl_zdemo_nas IMPLEMENTATION.

  METHOD DISPLAY_ALV.

    CREATE OBJECT W_CONTAINER
      EXPORTING
*    PARENT                      =
        CONTAINER_NAME              = 'CUSTOM'
*    STYLE                       =
*    LIFETIME                    = lifetime_default
*    REPID                       =
*    DYNNR                       =
*    NO_AUTODEF_PROGID_DYNNR     =
      EXCEPTIONS
        CNTL_ERROR                  = 1
        CNTL_SYSTEM_ERROR           = 2
        CREATE_ERROR                = 3
        LIFETIME_ERROR              = 4
        LIFETIME_DYNPRO_DYNPRO_LINK = 5
        others                      = 6
        .
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CREATE OBJECT W_GRID
      EXPORTING
*    I_SHELLSTYLE      = 0
*    I_LIFETIME        =
        I_PARENT          = W_CONTAINER
*    I_APPL_EVENTS     = space
*    I_PARENTDBG       =
*    I_APPLOGPARENT    =
*    I_GRAPHICSPARENT  =
*    I_NAME            =
*    I_FCAT_COMPLETE   = SPACE
      EXCEPTIONS
        ERROR_CNTL_CREATE = 1
        ERROR_CNTL_INIT   = 2
        ERROR_CNTL_LINK   = 3
        ERROR_DP_CREATE   = 4
        others            = 5
        .

    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CALL METHOD TABLE_DISPLAY.


  ENDMETHOD.                    "DISPLAY_ALV

*----------------------------------------------------------------------*

  METHOD TABLE_DISPLAY.

    select  vbeln posnr matnr matwa
            into table t_vbap
            from vbap
            where vbeln eq p_vbeln.

    CHECK SY-SUBRC EQ 0.

    loop at t_vbap assigning <fs_vbap>.

      clear wa_output_alv.
      wa_output_alv-vbeln = <fs_vbap>-vbeln.
      wa_output_alv-posnr = <fs_vbap>-posnr.
      wa_output_alv-matnr = <fs_vbap>-matnr.
      wa_output_alv-matwa = <fs_vbap>-matwa.

      APPEND wa_output_alv TO t_output_alv.

    endloop.

    UNASSIGN <fs_vbap>.

    CALL METHOD FIELD_CATALOG.

    CALL METHOD W_GRID->SET_TABLE_FOR_FIRST_DISPLAY
*   EXPORTING
*      I_BUFFER_ACTIVE               =
*      I_BYPASSING_BUFFER            =
*      I_CONSISTENCY_CHECK           =
*     I_STRUCTURE_NAME              = 'T_VBAP'
*      IS_VARIANT                    =
*      I_SAVE                        =
*      I_DEFAULT                     = 'X'
*      IS_LAYOUT                     =
*      IS_PRINT                      =
*      IT_SPECIAL_GROUPS             =
*      IT_TOOLBAR_EXCLUDING          =
*      IT_HYPERLINK                  =
*      IT_ALV_GRAPHICS               =
*      IT_EXCEPT_QINFO               =
*      IR_SALV_ADAPTER               =
       CHANGING
         IT_OUTTAB                     = t_output_alv
         IT_FIELDCATALOG               = t_fieldcat
*      IT_SORT                       =
*      IT_FILTER                     =
       EXCEPTIONS
         INVALID_PARAMETER_COMBINATION = 1
         PROGRAM_ERROR                 = 2
         TOO_MANY_LINES                = 3
         others                        = 4
             .
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    refresh t_vbap.
    refresh t_output_alv.
    UNASSIGN <fs_vbap>.

  ENDMETHOD.                    "TABLE_DISPLAY

*----------------------------------------------------------------------*

  METHOD FIELD_CATALOG.

    REFRESH t_fieldcat.
    CLEAR wa_fieldcat.

**** Sales Document ****
    wa_fieldcat-fieldname = 'VBELN'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-001.
    wa_fieldcat-outputlen = 15.
    APPEND wa_fieldcat TO t_fieldcat.

**** Sales Document Item ****

    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'POSNR'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-002.
    wa_fieldcat-outputlen = 20.
    APPEND wa_fieldcat TO t_fieldcat.

**** Material Number ****

    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MATNR'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-003.
    wa_fieldcat-outputlen = 15.
    APPEND wa_fieldcat TO t_fieldcat.

**** Material entered ****

    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MATWA'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-004.
    wa_fieldcat-outputlen = 15.
    APPEND wa_fieldcat TO t_fieldcat.

  ENDMETHOD.                    "FIELD_CATALOG

*----------------------------------------------------------------------*
  
ENDCLASS.                    "lcl_zdemo_nas IMPLEMENTATION


5. Now that the class definition and implementation is done, we have to create an instance w_obj of the class and define one screen, which will hold the container to display ALV.

*----------------------------------------------------------------------*
*       PROGRAM PROCESSING BEGINS                        *
*----------------------------------------------------------------------*
INITIALIZATION.
   DATA:  w_obj TYPE REF TO lcl_zdemo_nas.
   CREATE OBJECT w_obj.
   INCLUDE ZDEMO_NAS_STATUS_1234O01. "PBO
   INCLUDE ZDEMO_NAS_USER_COMMAND_1234I01. "PAI

START-OF-SELECTION.
    CALL SCREEN 1234. "CUSTOM CONTAINER

6. Now double click on the screen number 1234. Control will take you to the Screen Painter. Click on the layout and create the Custom Control Element.


7. Come back to the Screen Painter and in the Element List tab provide the name for  Custom Control as  ‘CUSTOM’ and rename the element  code as OK_CODE.



8. Now go to the Flow logic tab and uncomment the PBO and PAI Module Statuses.



9. Now double click on MODULE STATUS_1234 of PBO. It will ask tell you a new include program will be added to your program click OK. Provide the PF-STATUS and the TITLEBAR. Double click on the status 'Z_REPORT' and activate the Function Keys BACK, CANCEL and EXIT. After that call the method display_alv.


10. Come back to the initial screen of the Screen Painter and double click on MODULE USER_COMMAND_1234 of PAI. And write the logic for action to be performed when BACK, CANCEL and EXIT buttons are clicked.




************************************************************
Appendix: Program ZDEMO_NAS
************************************************************


REPORT  ZDEMO_NAS NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       GLOBAL DECLARATIONS                                            *
*----------------------------------------------------------------------*

TYPES: begin of ty_vbap,
       VBELN type vbeln_va,
       POSNR type POSNR_VA,
       MATNR type MATNR,
       MATWA type MATWA,
       end of ty_vbap.

FIELD-SYMBOLS: <fs_vbap> type ty_vbap.

*----------------------------------------------------------------------*
*       SELECTION SCREEN                                               *
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 with frame title text-000 no intervals.
 PARAMETER: p_vbeln TYPE vbap-vbeln.
 SELECTION-SCREEN END OF BLOCK B1.
  
*----------------------------------------------------------------------*
*       CLASS lcl_zdemo_nas DEFINITION                                 *
*----------------------------------------------------------------------*
 CLASS lcl_zdemo_nas DEFINITION.
   Public Section.
     METHODS: DISPLAY_ALV.
   
Private Section.
     DATA: t_vbap type table of ty_vbap,
          t_output_alv type table of ty_vbap,
          wa_output_alv type ty_vbap.
     
      DATA: w_grid TYPE REF TO CL_GUI_ALV_GRID,
          w_container TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
          t_fieldcat type LVC_T_FCAT,
          wa_fieldcat type LVC_S_FCAT.
     
      METHODS: 
             TABLE_DISPLAY,
             FIELD_CATALOG.

ENDCLASS.                    "lcl_zdemo_nas DEFINITION
  
*----------------------------------------------------------------------*
*       CLASS lcl_zdemo_nas IMPLEMENTATION                             *
*----------------------------------------------------------------------*

CLASS lcl_zdemo_nas IMPLEMENTATION.

  METHOD DISPLAY_ALV.

    CREATE OBJECT W_CONTAINER
      EXPORTING
*    PARENT                      =
        CONTAINER_NAME              = 'CUSTOM'
*    STYLE                       =
*    LIFETIME                    = lifetime_default
*    REPID                       =
*    DYNNR                       =
*    NO_AUTODEF_PROGID_DYNNR     =
      EXCEPTIONS
        CNTL_ERROR                  = 1
        CNTL_SYSTEM_ERROR           = 2
        CREATE_ERROR                = 3
        LIFETIME_ERROR              = 4
        LIFETIME_DYNPRO_DYNPRO_LINK = 5
        others                      = 6
        .
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CREATE OBJECT W_GRID
      EXPORTING
*    I_SHELLSTYLE      = 0
*    I_LIFETIME        =
        I_PARENT          = W_CONTAINER
*    I_APPL_EVENTS     = space
*    I_PARENTDBG       =
*    I_APPLOGPARENT    =
*    I_GRAPHICSPARENT  =
*    I_NAME            =
*    I_FCAT_COMPLETE   = SPACE
      EXCEPTIONS
        ERROR_CNTL_CREATE = 1
        ERROR_CNTL_INIT   = 2
        ERROR_CNTL_LINK   = 3
        ERROR_DP_CREATE   = 4
        others            = 5
        .

    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CALL METHOD TABLE_DISPLAY.


  ENDMETHOD.                    "DISPLAY_ALV

*----------------------------------------------------------------------*

  METHOD TABLE_DISPLAY.

    select  vbeln posnr matnr matwa
            into table t_vbap
            from vbap
            where vbeln eq p_vbeln.

    CHECK SY-SUBRC EQ 0.

    loop at t_vbap assigning <fs_vbap>.

      clear wa_output_alv.
      wa_output_alv-vbeln = <fs_vbap>-vbeln.
      wa_output_alv-posnr = <fs_vbap>-posnr.
      wa_output_alv-matnr = <fs_vbap>-matnr.
      wa_output_alv-matwa = <fs_vbap>-matwa.

      APPEND wa_output_alv TO t_output_alv.

    endloop.

    UNASSIGN <fs_vbap>.

    CALL METHOD FIELD_CATALOG.

    CALL METHOD W_GRID->SET_TABLE_FOR_FIRST_DISPLAY
*    EXPORTING
*      I_BUFFER_ACTIVE               =
*      I_BYPASSING_BUFFER            =
*      I_CONSISTENCY_CHECK           =
*      I_STRUCTURE_NAME              =
*      IS_VARIANT                    =
*      I_SAVE                        =
*      I_DEFAULT                     = 'X'
*      IS_LAYOUT                     =
*      IS_PRINT                      =
*      IT_SPECIAL_GROUPS             =
*      IT_TOOLBAR_EXCLUDING          =
*      IT_HYPERLINK                  =
*      IT_ALV_GRAPHICS               =
*      IT_EXCEPT_QINFO               =
*      IR_SALV_ADAPTER               =
       CHANGING
         IT_OUTTAB                     = t_output_alv
         IT_FIELDCATALOG               = t_fieldcat
*      IT_SORT                       =
*      IT_FILTER                     =
       EXCEPTIONS
         INVALID_PARAMETER_COMBINATION = 1
         PROGRAM_ERROR                 = 2
         TOO_MANY_LINES                = 3
         others                        = 4
             .
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    refresh t_vbap.
    refresh t_output_alv.
    UNASSIGN <fs_vbap>.

  ENDMETHOD.                    "TABLE_DISPLAY

*----------------------------------------------------------------------*

  METHOD FIELD_CATALOG.

    REFRESH t_fieldcat.
    CLEAR wa_fieldcat.
  
**** Sales Document **** 
    wa_fieldcat-fieldname = 'VBELN'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-001.
    wa_fieldcat-outputlen = 15.
    APPEND wa_fieldcat TO t_fieldcat.

**** Sales Document Item **** 
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'POSNR'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-002.
    wa_fieldcat-outputlen = 20.
    APPEND wa_fieldcat TO t_fieldcat.

**** Material Number **** 
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MATNR'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-003.
    wa_fieldcat-outputlen = 15.
    APPEND wa_fieldcat TO t_fieldcat.

**** Material entered **** 
    CLEAR wa_fieldcat.
    wa_fieldcat-fieldname = 'MATWA'.
    wa_fieldcat-tabname   = 'T_OUTPUT_ALV'.
    wa_fieldcat-coltext   = text-004.
    wa_fieldcat-outputlen = 15.
    APPEND wa_fieldcat TO t_fieldcat.

  ENDMETHOD.                    "FIELD_CATALOG

*----------------------------------------------------------------------*

ENDCLASS.                    "lcl_zdemo_nas IMPLEMENTATION


*----------------------------------------------------------------------*
*       PROGRAM PROCESSING BEGINS                                      *
*----------------------------------------------------------------------*
INITIALIZATION. 
  DATA:  w_obj TYPE REF TO lcl_zdemo_nas.
   CREATE OBJECT w_obj.

  INCLUDE ZDEMO_NAS_STATUS_1234O01. "PBO
  INCLUDE ZDEMO_NAS_USER_COMMAND_1234I01. "PAI

START-OF-SELECTION.
   CALL SCREEN 1234. "CUSTOM CONTAINER

*********************************************************





2 comments: