Tuesday, May 5, 2020

ALV Report with drill down option

ALV report is created using the standard function modules provided by SAP.

ALV report can be created using the following steps.

  • Include SLIS type pool – SLIS type pool contains all the data types required by ALV function modules.
  • Data retrieval – Code the logic to fetch the data from database table into an Internal Table.
  • Build Field Catalog – Add the columns into an internal that you want to display in the ALV output list.
  • Pass the data table and field catalog table to ALV function module 

*&---------------------------------------------------------------------*
*& Report ZTEST_PROGRAM_ALV
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*

INCLUDE ztest_program_alv_top                   .    " Global Data

INCLUDE ztest_program_alv_f01                   .  " FORM-Routines

INITIALIZATION.

AT SELECTION-SCREEN.

START-OF-SELECTION.

  PERFORM f_get_data.

END-OF-SELECTION.

  IF NOT it_vbak IS INITIAL.

    PERFORM f_fieldcat_alv.

    PERFORM f_display_alv.

  ENDIF.

---------------------------------------------------------------------------
*&---------------------------------------------------------------------*
*& Include ZTEST_PROGRAM_ALV_TOP                    - Report ZTEST_PROGRAM_ALV
*&---------------------------------------------------------------------*
REPORT ztest_program_alv.

TABLES vbak.

TYPE-POOLS slis.
DATA gs_vbak TYPE vbak.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.

SELECT-OPTIONS s_vbeln FOR gs_vbak-vbeln." OBLIGATORY.

SELECTION-SCREEN END OF BLOCK b1.

TYPESBEGIN OF ty_vbak,
         check TYPE c1,
         mandt TYPE sy-mandt,
         vbeln TYPE vbeln_va,
         erdat TYPE erdat,
         erzet TYPE erzet,
         ernam TYPE ernam,
         angdt TYPE angdt_v,
         bnddt TYPE bnddt,
         audat TYPE audat,
         vbtyp TYPE vbtypl,
         trvog TYPE trvog,
         auart TYPE auart,
         augru TYPE augru,
       END OF ty_vbak.


TYPESBEGIN OF ty_vbap,
         vbeln TYPE vbeln_va,
         posnr TYPE posnr_va,
         matnr TYPE matnr,
         matwa TYPE matwa,
         pmatn TYPE pmatn,
         charg TYPE charg_d,
         matkl TYPE matkl,
         arktx TYPE arktx,
       END OF ty_vbap,

       BEGIN OF ty_final,
         vbeln TYPE vbeln_va,
         posnr TYPE posnr_va,
         matnr TYPE matnr,
         matwa TYPE matwa,
         pmatn TYPE pmatn,
         charg TYPE charg_d,
         matkl TYPE matkl,
         arktx TYPE arktx,
         maktx TYPE maktx,
       END OF ty_final,

       BEGIN OF ty_makt,
         matnr TYPE matnr,
         spras TYPE spras,
         maktx TYPE maktx,
       END OF ty_makt.

DATAit_vbak  TYPE TABLE OF ty_vbak,
      wa_vbak  TYPE          ty_vbak,
      it_vbap  TYPE TABLE OF ty_vbap,
      wa_vbap  TYPE          ty_vbap,
      it_final TYPE TABLE OF ty_final,
      wa_final TYPE          ty_final,
      it_makt  TYPE TABLE OF ty_makt,
      wa_makt  TYPE          ty_makt,

      it_fcat  TYPE slis_t_fieldcat_alv,
      it_fcat1 TYPE slis_t_fieldcat_alv,
      wa_fcat  TYPE slis_fieldcat_alv.

----------------------------------------------------------------------------

*&---------------------------------------------------------------------*
*& Include          ZTEST_PROGRAM_ALV_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form F_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM f_get_data .

  DATAlt_vbap TYPE TABLE OF ty_vbap.

  REFRESH it_vbak.
  CLEAR :  wa_vbak.

  IF NOT s_vbeln IS INITIAL.
    SELECT  mandt
            vbeln
            erdat
            erzet
            ernam
            angdt
            bnddt
            audat
            vbtyp
            trvog
            auart
            augru
      FROM vbak
*      INTO TABLE it_vbak
      INTO CORRESPONDING FIELDS OF TABLE it_vbak
      WHERE vbeln IN s_vbeln.
  ELSE.
    SELECT  mandt
            vbeln
            erdat
            erzet
            ernam
            angdt
            bnddt
            audat
            vbtyp
            trvog
            auart
            augru
  FROM vbak
*  INTO TABLE it_vbak
      INTO CORRESPONDING FIELDS OF TABLE it_vbak
  UP TO 1000 ROWS.
  ENDIF.

  IF NOT it_vbak IS INITIAL.
    SORT it_vbak BY vbeln.

    SELECT  vbeln
            posnr
            matnr
            matwa
            pmatn
            charg
            matkl
            arktx
      FROM vbap
      INTO TABLE it_vbap
      FOR ALL ENTRIES IN it_vbak
      WHERE vbeln it_vbak-vbeln.

    IF sy-subrc IS INITIAL
      AND NOT it_vbap IS INITIAL.
      SORT it_vbap BY vbeln posnr.
      lt_vbap[] it_vbap[].
      SORT lt_vbap BY matnr.
      DELETE ADJACENT DUPLICATES FROM lt_vbap COMPARING matnr.
      IF sy-subrc IS INITIAL
        AND NOT lt_vbap IS INITIAL.
        SELECT matnr
                spras
                maktx
          FROM makt
          INTO TABLE it_makt
          FOR ALL ENTRIES IN lt_vbap
          WHERE matnr lt_vbap-matnr
          AND spras 'E'.
        IF sy-subrc IS INITIAL
          AND NOT it_makt IS INITIAL.
          SORT it_makt BY matnr.
          LOOP AT it_vbap INTO wa_vbap.
            wa_final-vbeln wa_vbap-vbeln.
            wa_final-posnr wa_vbap-posnr.
            wa_final-matnr wa_vbap-matnr.
            wa_final-matwa wa_vbap-matwa.
            wa_final-pmatn wa_vbap-pmatn.
            wa_final-charg wa_vbap-charg.
            wa_final-matkl wa_vbap-matkl.
            wa_final-arktx wa_vbap-arktx.

            READ TABLE it_makt INTO wa_makt
            WITH KEY matnr wa_vbap-matnr
            BINARY SEARCH.
            IF sy-subrc IS INITIAL.
              wa_final-maktx wa_makt-maktx.
            ENDIF.
            APPEND wa_final TO it_final.
            CLEAR wa_final.
          ENDLOOP.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form F_FIELDCAT_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM f_fieldcat_alv .

  REFRESHit_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '1'.
  wa_fcat-fieldname 'CHECK'.
  wa_fcat-checkbox 'X'.
  wa_fcat-edit 'X'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m 'Check Box'."TEXT-002.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '2'.
  wa_fcat-fieldname 'VBELN'.
  wa_fcat-edit 'X'.
  wa_fcat-hotspot 'X'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m 'Sales Document'."TEXT-002.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '3'.
  wa_fcat-fieldname 'ERDAT'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_l TEXT-003.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '4'.
  wa_fcat-fieldname 'ERZET'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-004.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '5'.
  wa_fcat-fieldname 'ERNAM'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-005.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '6'.
  wa_fcat-fieldname 'ANGDT'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-006.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '7'.
  wa_fcat-fieldname 'BNDDT'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-007.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '8'.
  wa_fcat-fieldname 'AUDAT'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-008.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '9'.
  wa_fcat-fieldname 'VBTYP'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-009.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '10'.
  wa_fcat-fieldname 'TRVOG'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-010.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '11'.
  wa_fcat-fieldname 'AUART'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-011.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '12'.
  wa_fcat-fieldname 'AUGRU'.
  wa_fcat-tabname 'IT_VBAK'.
  wa_fcat-seltext_m TEXT-012.
  APPEND wa_fcat TO it_fcat.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '1'.
  wa_fcat-fieldname 'VBELN'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-013.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '2'.
  wa_fcat-fieldname 'POSNR'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-014.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '3'.
  wa_fcat-fieldname 'MATNR'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-015.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '4'.
  wa_fcat-fieldname 'MATWA'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-016.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '5'.
  wa_fcat-fieldname 'PMATN'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-017.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '6'.
  wa_fcat-fieldname 'CHARG'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-018.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '7'.
  wa_fcat-fieldname 'MATKL'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-019.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '8'.
  wa_fcat-fieldname 'ARKTX'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-020.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.

  wa_fcat-row_pos '1'.
  wa_fcat-col_pos '9'.
  wa_fcat-fieldname 'MAKTX'.
  wa_fcat-tabname 'IT_FINAL'.
  wa_fcat-seltext_m TEXT-021.
  APPEND wa_fcat TO it_fcat1.
  CLEAR wa_fcat.


ENDFORM.
*&---------------------------------------------------------------------*
*& Form F_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM f_display_alv.

  DATAlv_repid TYPE sy-repid.

  lv_repid sy-repid.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING

      i_callback_program       lv_repid
      i_callback_pf_status_set 'SET_PF_STATUS'
      i_callback_user_command  'DISPLAY_ITEMS'
      it_fieldcat              it_fcat
      i_save                   'A'
    TABLES
      t_outtab                 it_vbak
    EXCEPTIONS
      program_error            1
      OTHERS                   2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.

FORM display_items USING ucomm TYPE sy-ucomm
                         selfield TYPE slis_selfield.


  DATAlt_vbap  TYPE TABLE OF ty_vbap,
        lt_final TYPE TABLE OF ty_final,
        ls_vbak  TYPE   ty_vbak,
        lr_grid  TYPE REF TO cl_gui_alv_grid.

  CASE ucomm.
    WHEN '&IC1'.

****      drill down to the transaction
**      READ TABLE it_vbak INTO wa_vbak INDEX selfield-tabindex.
**      SET PARAMETER ID 'AUN' FIELD wa_vbak-vbeln.
**      CALL TRANSACTION 'VA03'  AND  SKIP FIRST SCREEN.
****End of drill down

*      lt_vbap[] = it_vbap[].
      lt_final[] it_final[].
      READ TABLE it_vbak INTO ls_vbak INDEX selfield-tabindex.
      IF sy-subrc IS INITIAL.
*        DELETE lt_vbap WHERE vbeln NE ls_vbak-vbeln.
        DELETE lt_final WHERE vbeln NE ls_vbak-vbeln.
*               IF NOT lt_vbap IS INITIAL.
        IF NOT lt_final IS INITIAL.
          CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
            EXPORTING
              i_callback_program sy-repid
              it_fieldcat        it_fcat1
              i_save             'A'
            TABLES
              t_outtab           lt_final
            EXCEPTIONS
              program_error      1
              OTHERS             2.
          IF sy-subrc <> 0.
* Implement suitable error handling here
          ENDIF.
        ENDIF.
      ENDIF.
    WHEN 'SAVE' OR 'ADD'.
*      BREAK-POINT.
      READ TABLE it_vbak INTO wa_vbak INDEX selfield-tabindex.
      wa_vbak-check 'x'.
      MODIFY it_vbak FROM wa_vbak INDEX selfield-tabindex.
      selfield-refresh 'X'.
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
*       EXPORTING
*         IR_SALV_FULLSCREEN_ADAPTER       =
        IMPORTING
*         ET_EXCLUDING                     =
*         E_REPID                          =
*         E_CALLBACK_PROGRAM               =
*         E_CALLBACK_ROUTINE               =
          e_grid lr_grid
*         ET_FIELDCAT_LVC                  =
*         ER_TRACE                         =
*         E_FLG_NO_HTML                    =
*         ES_LAYOUT_KKBLO                  =
*         ES_SEL_HIDE                      =
*         ET_EVENT_EXIT                    =
*         ER_FORM_TOL                      =
*         ER_FORM_EOL                      =

        .
************      To check the last changed option
*      CALL METHOD lr_grid->refresh_table_display
**        EXPORTING
**          is_stable      =
**          i_soft_refresh =
**        EXCEPTIONS
**          finished       = 1
**          others         = 2
*        .
**      IF sy-subrc <> 0.
***       Implement suitable error handling here
**      ENDIF.

**********To check the multiple check box option
      CALL METHOD lr_grid->check_changed_data
*  IMPORTING
*    e_valid   =
*  CHANGING
*    c_refresh = 'X'
        .



      LOOP AT it_vbak INTO wa_vbak.
*        BREAK-POINT.
        IF wa_vbak-check 'X'.
**       DELETE zvbak FROM TABLE it_vbak.
*        DELETE zvbak FROM wa_vbak.
          UPDATE vbak SET truck ' ' WHERE vbeln wa_vbak-vbeln.
          IF sy-subrc IS INITIAL.
            COMMIT WORK.
            MESSAGE 'Records updated' TYPE 'S'.
          ELSE.
            ROLLBACK WORK.
          ENDIF.
        ELSE.
          ROLLBACK WORK.
        ENDIF.

*
      ENDLOOP.
  ENDCASE.

ENDFORM.

FORM set_pf_status USING rt_extab TYPE slis_t_extab.
*BREAK-POINT.
  SET PF-STATUS 'ZSTANDARD'.
ENDFORM.

--------------------------------------------------------------------------
Input Screen:







Output with Drill down option:



No comments:

Post a Comment