INTERACTIVE & EDITABLE ALV REPORT in SAP
SAP ABAP Interview Questions – Real Time
Step 1: Go to SE38 Tcode. Type the program name and choose Source Code from Subobjects and Click on Create button.
Step 2: Enter the Title & select ‘Executable Program’ type in Attributes and then click on save.
Step 3: Now enter the Package name in the ‘Package’ Attribute and click on the button.
Step 4: Select the TR and click on .
Now ABAP Editor will open, here write the code given below for report.
REPORT zalvexample7.
*Table Declaration
TABLES : vbak, vbap.
*Type-Pools
TYPE-POOLS: slis.
*Type Declaration
TYPES : BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE vbak-erdat,
ernam TYPE vbak-ernam,
auart TYPE vbak-auart,
vkorg TYPE vbak-vkorg,
vtweg TYPE vbak-vtweg,
spart TYPE vbak-spart,
vkgrp TYPE vbak-vkgrp,
kunnr TYPE vbak-kunnr,
END OF ty_vbak,
BEGIN OF ty_vbap,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
matnr TYPE vbap-matnr,
matkl TYPE vbap-matkl,
arktx TYPE vbap-arktx,
netwr TYPE vbap-netwr,
waerk TYPE vbap-waerk,
klmeng TYPE vbap-klmeng,
vrkme TYPE vbap-vrkme,
END OF ty_vbap.
*Data Declaration
DATA : gt_vbak TYPE STANDARD TABLE OF ty_vbak,
wa_vbak TYPE ty_vbak,
gt_vbap TYPE STANDARD TABLE OF ty_vbap,
wa_vbap TYPE ty_vbap.
DATA : gt_fcat TYPE slis_t_fieldcat_alv,
wa_fcat LIKE LINE OF gt_fcat,
gt_layout TYPE slis_layout_alv.
*Selection Screen
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS : so_vbeln FOR vbak-vbeln.
SELECTION-SCREEN END OF BLOCK b1.
START-OF-SELECTION.
*Data Fetching
SELECT vbeln
erdat
ernam
auart
vkorg
vtweg
spart
vkgrp
kunnr
FROM vbak INTO TABLE gt_vbak
WHERE vbeln IN so_vbeln.
SORT gt_vbak BY vbeln.
*Field Catalog
wa_fcat-col_pos = 1.
wa_fcat-fieldname = ‘VBELN’.
wa_fcat-seltext_m = ‘SALES DOC. NO.’.
wa_fcat-hotspot = ‘X’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 2.
wa_fcat-fieldname = ‘ERDAT’.
wa_fcat-seltext_m = ‘CREATED ON’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 3.
wa_fcat-fieldname = ‘ERNAM’.
wa_fcat-seltext_m = ‘CREATED BY’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 4.
wa_fcat-fieldname = ‘AUART’.
wa_fcat-seltext_m = ‘DOC. TYPE’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 5.
wa_fcat-fieldname = ‘VKORG’.
wa_fcat-seltext_m = ‘SALES ORG.’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 6.
wa_fcat-fieldname = ‘VTWEG’.
wa_fcat-seltext_m = ‘D. CHANNEL’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 7.
wa_fcat-fieldname = ‘SPART’.
wa_fcat-seltext_m = ‘DIVISION’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 8.
wa_fcat-fieldname = ‘VKGRP’.
wa_fcat-seltext_m = ‘SALES GROUP’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 9.
wa_fcat-fieldname = ‘KUNNR’.
wa_fcat-seltext_m = ‘CUSTOMER’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
*LAYOUT
gt_layout-colwidth_optimize = ‘X’.
gt_layout-edit = ‘X’.
* DISPLAY FIRST ALV Grid
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
i_callback_user_command = ‘USER_COMMAND’
is_layout = gt_layout
it_fieldcat = gt_fcat
TABLES
t_outtab = gt_vbak
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*User Command
FORM user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN ‘&IC1’.
READ TABLE gt_vbak INTO wa_vbak INDEX rs_selfield-tabindex.
IF sy-subrc = 0.
*Fetching Data of Second Table
SELECT vbeln
posnr
matnr
matkl
arktx
netwr
waerk
klmeng
vrkme
FROM vbap INTO TABLE gt_vbap
WHERE vbeln = wa_vbak-vbeln.
SORT gt_vbap BY vbeln.
* FIELDCATALOG
CLEAR : gt_fcat, wa_fcat.
*Fieldcatalog of Second Table
wa_fcat-col_pos = 1.
wa_fcat-fieldname = ‘VBELN’.
wa_fcat-seltext_m = ‘SALES DOC. NO.’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 2.
wa_fcat-fieldname = ‘POSNR’.
wa_fcat-seltext_m = ‘ITEM NO.’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 3.
wa_fcat-fieldname = ‘MATNR’.
wa_fcat-seltext_m = ‘MATERIAL NO.’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 4.
wa_fcat-fieldname = ‘MATKL’.
wa_fcat-seltext_m = ‘MATERIAL GROUP’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 5.
wa_fcat-fieldname = ‘ARKTX’.
wa_fcat-seltext_m = ‘DESCRIPTIONS’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 6.
wa_fcat-fieldname = ‘NETWR’.
wa_fcat-seltext_m = ‘AMOUNT’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 7.
wa_fcat-fieldname = ‘WAERK’.
wa_fcat-seltext_m = ‘CURRENCY’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 8.
wa_fcat-fieldname = ‘KLMENG’.
wa_fcat-seltext_m = ‘QUANTITY’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
wa_fcat-col_pos = 9.
wa_fcat-fieldname = ‘VRKME’.
wa_fcat-seltext_m = ‘SALES UNIT’.
APPEND wa_fcat TO gt_fcat.
CLEAR wa_fcat.
* LAYOUT
gt_layout-colwidth_optimize = ‘X’.
gt_layout-edit = ‘X’.
* DISPLAY Second ALV Grid
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
is_layout = gt_layout
it_fieldcat = gt_fcat
TABLES
t_outtab = gt_vbap
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
ENDCASE.
ENDFORM.
OUTPUT
Step 1: Press F8.
Step 2: Press F8 to execute the report.
Step 3: Now click on any Sales Document number to display the secondary list.