Object Oriented ALV Report for displaying records from a table
SAP ABAP Interview Questions – Real Time
Step 1: Go to SE38 Tcode. Type the program name and choose Source Code from Sub-objects and Click on Create button.
Step 2: A new pop-up will display. Write the Title & select ‘Executable Program’ type in Attributes and then click on save.
Step 3: Now enter the package name in ‘Package’ attribute and click on Save button.
Step 4: A new pop-up will open & select the TR and click on the continue button.
Now ABAP Editor will open, here write the code given below for report.
Step 5: Click on Display Object List as marked above screen & right-click on Program name and Create Screen
Step 6: Now type here given codes Code.
*&———————————————————————*
*& Report ZOOALVSAMPLE
*&———————————————————————*
*&
*&———————————————————————*
REPORT zooalvsample.
INCLUDE zooalvsample_top.
START-OF-SELECTION.
PERFORM fetch_data.
*&———————————————————————*
*& Module STATUS_0100 OUTPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE status_0100 OUTPUT.
SET PF-STATUS ‘ZSTATUS’.
SET TITLEBAR ‘ZTITLE’.
ENDMODULE. ” STATUS_0100 OUTPUT
*&———————————————————————*
*& Module alv_grid OUTPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE alv_grid OUTPUT.
CREATE OBJECT c_ccont
EXPORTING
container_name = ‘CC_CONT’.
CREATE OBJECT c_alvgd
EXPORTING
i_parent = c_ccont.
PERFORM alv_build_fieldcat.
PERFORM alv_report_layout.
CHECK NOT c_alvgd IS INITIAL.
CALL METHOD c_alvgd->set_table_for_first_display
EXPORTING
is_layout = it_layout
CHANGING
it_outtab = lt_mara
it_fieldcatalog = it_fcat
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.
ENDMODULE. ” USER_COMMAND_0100 INPUT
*&———————————————————————*
*& Form FETCH_DATA
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM fetch_data .
REFRESH lt_mara[].
SELECT
matnr
ersda
ernam
mtart
mbrsh
matkl
FROM mara UP TO 20 ROWS INTO CORRESPONDING FIELDS OF TABLE lt_mara
WHERE matnr IN s_matnr.
IF lt_mara[] IS NOT INITIAL.
CALL SCREEN 100.
ELSE.
MESSAGE ‘Data Not Found’ TYPE ‘S’ DISPLAY LIKE ‘E’.
ENDIF.
ENDFORM. ” FETCH_DATA
*&———————————————————————*
*& Form ALV_BUILD_FIELDCAT
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM alv_build_fieldcat .
DATA lv_fldcat TYPE lvc_s_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = ‘1’.
lv_fldcat-col_pos = ‘1’.
lv_fldcat-fieldname = ‘MATNR’.
lv_fldcat-tabname = ‘LT_MARA’.
lv_fldcat-outputlen = 10.
lv_fldcat-scrtext_m = ‘Material’.
lv_fldcat-icon = ‘X’.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = ‘1’.
lv_fldcat-col_pos = ‘2’.
lv_fldcat-fieldname = ‘ERSDA’.
lv_fldcat-tabname = ‘LT_MARA’.
lv_fldcat-outputlen = 10.
lv_fldcat-scrtext_m = ‘Created On’.
lv_fldcat-icon = ‘X’.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = ‘1’.
lv_fldcat-col_pos = ‘3’.
lv_fldcat-fieldname = ‘ERNAM’.
lv_fldcat-tabname = ‘LT_MARA’.
lv_fldcat-outputlen = 5.
lv_fldcat-scrtext_m = ‘Created By’.
lv_fldcat-icon = ‘X’.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = ‘1’.
lv_fldcat-col_pos = ‘4’.
lv_fldcat-fieldname = ‘MTART’.
lv_fldcat-tabname = ‘LT_MARA’.
lv_fldcat-outputlen = 18.
lv_fldcat-scrtext_m = ‘Material type’.
lv_fldcat-icon = ‘X’.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = ‘1’.
lv_fldcat-col_pos = ‘5’.
lv_fldcat-fieldname = ‘MBRSH’.
lv_fldcat-tabname = ‘LT_MARA’.
lv_fldcat-outputlen = 2.
lv_fldcat-scrtext_m = ‘Industry Sector’.
lv_fldcat-icon = ‘X’.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = ‘1’.
lv_fldcat-col_pos = ‘6’.
lv_fldcat-fieldname = ‘MATKL’.
lv_fldcat-tabname = ‘LT_MARA’.
lv_fldcat-outputlen = 9.
lv_fldcat-scrtext_m = ‘Material Group’.
lv_fldcat-icon = ‘X’.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
ENDFORM. ” ALV_BUILD_FIELDCAT
*&———————————————————————*
*& Form ALV_REPORT_LAYOUT
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM alv_report_layout .
it_layout-cwidth_opt = ‘X’.
it_layout-zebra = ‘X’.
it_layout-col_opt = ‘X’.
ENDFORM. ” ALV_REPORT_LAYOUT
*&———————————————————————*
*& Module USER_COMMAND_0100 INPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE user_command_0100 INPUT.
CASE : sy-ucomm.
WHEN ‘BACK’.
LEAVE TO SCREEN 0.
WHEN ‘EXIT’ OR ‘CANCEL’.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE. ” USER_COMMAND_0100 INPUT
Step 7: Create One Include Program by Double Click on Include Name.
INCLUDE zooalvsample_top.
And copy the code
*&———————————————————————*
*& Include ZOOALVSAMPLE_TOP
*&———————————————————————*
TABLES : mara.
SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS : s_matnr FOR mara-matnr.
SELECTION-SCREEN : END OF BLOCK b1.
*Data declarations for ALV
DATA: c_ccont TYPE REF TO cl_gui_custom_container, “Custom container
c_alvgd TYPE REF TO cl_gui_alv_grid, “ALV grid object
it_fcat TYPE lvc_t_fcat, “Field catalogue
it_layout TYPE lvc_s_layo. “Layout
TYPES : BEGIN OF ty_mara,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
mtart TYPE mara-mtart,
mbrsh TYPE mara-mbrsh,
matkl TYPE mara-matkl,
END OF ty_mara.
DATA : lt_mara TYPE TABLE OF ty_mara INITIAL SIZE 0.
Step 8: Now double click on Screen Number.
And put this code.
PROCESS BEFORE OUTPUT.
MODULE status_0100.
MODULE alv_grid.
PROCESS AFTER INPUT.
MODULE user_command_0100.
Step 9: Click on the Layout button .
Step 10: Now Create a custom container CC_CONT on the layout.
Step 11: Create Status as below screen.
Step 11: Press F8 button to execute the program & now you can see the output of this OOALV Report.