Normally the ALV output is displayed in a new screen. Not on the same screen where you have the selection parameters and select options. But, sometimes client want to see what they input in the screen and what they get output in the ALV in the same screen. It just helps them perform better, report better and analyse better. This requirement and scenario is not new. We have solution for it from back then in 2008.
If the solution is already there, then why again a new article? SAPYard has to be different. The earlier solution provided uses SALV class “CL_SALV_TABLE“. We will use “CL_GUI_ALV_GRID” class due to its enhanced features.
Expectations
- Selection Screen and ALV on the Same Screen.
- Add a Button on the ALV Toolbar.
- Handle User Command.
For more information on the usage and functioning of the class CL_GUI_ALV_GRID, you can refer to the demo programs provided by SAP with the name like “BCALV_GRID* “.

Output

Below is the Final Output which the Client is Expecting.

Do you know, how to check the consistency of any ALV Report? Check this – Just a key and two clicks for ALV consistency check
Let’s Design
High Level Programming Paradigm:
In other words, Pseudo Code for this requirement.
- Docking Container needs to be created in the INITIALIZATION event.
- ALV needs to be created on that Docking Container.
- Export the SELECTed Output Table Data to ABAP Memory (this part we do not like. But till we find a better solution, let’s use it).
- Import data from ABAP Memory and Generate the Output.
Global Data Declarations
<code>CLASS lcl_report DEFINITION DEFERRED. DATA: lo_report TYPE REF TO lcl_report. "Reference object of the local class DATA: alv_container TYPE REF TO cl_gui_docking_container, "ALV Container alv_grid TYPE REF TO cl_gui_alv_grid, "ALV Grid layout TYPE lvc_s_layo. "Layout Options DATA gv_carrid TYPE sflight-carrid. "Variable for Select-options declaration.</code>
Selection Screen Declarations
<code>SELECTION-SCREEN: BEGIN OF BLOCK block_1 WITH FRAME TITLE text-001. SELECT-OPTIONS: s_carrid FOR gv_carrid. SELECTION-SCREEN: END OF BLOCK block_1.</code>
Local Class Definition
<code>CLASS lcl_report DEFINITION . PUBLIC SECTION. DATA: gt_data TYPE STANDARD TABLE OF sflight. METHODS : get_data, generate_output, toolbar FOR EVENT toolbar OF cl_gui_alv_grid IMPORTING e_object, "To add some buttons on the ALV toolbar user_command FOR EVENT user_command OF cl_gui_alv_grid IMPORTING e_ucomm. ENDCLASS.</code>
Local Class Implementation
- Method: GET_DATA
<code>CLASS lcl_report IMPLEMENTATION. "Method Get Data METHOD get_data. * data selection SELECT * FROM sflight INTO TABLE me->gt_data WHERE carrid IN s_carrid. IF sy-dbcnt IS INITIAL. MESSAGE s398(00) WITH 'No data selected'. ENDIF. * * Export to memory EXPORT data = me->gt_data TO MEMORY ID sy-cprog. ENDMETHOD.</code>
- Method: GENERATE_OUTPUT
<code>"Method generate_output METHOD generate_output. * Local data DATA: variant TYPE disvariant. DATA: repid TYPE sy-repid. * Import output table from the memory and free afterwards IMPORT data = me->gt_data FROM MEMORY ID sy-cprog. FREE MEMORY ID sy-cprog. * * Only if there is some data CHECK me->gt_data IS NOT INITIAL. repid = sy-repid. variant-report = sy-repid. variant-username = sy-uname. layout-zebra = 'X'. CHECK alv_container IS INITIAL. CREATE OBJECT alv_container EXPORTING repid = repid dynnr = sy-dynnr side = alv_container->dock_at_bottom extension = 200. CREATE OBJECT alv_grid EXPORTING i_parent = alv_container. * ALV Specific. Data selection. SET HANDLER : lo_report->toolbar FOR alv_grid. SET HANDLER : lo_report->user_command FOR alv_grid. CALL METHOD alv_grid->set_table_for_first_display EXPORTING is_layout = layout is_variant = variant i_save = 'U' i_structure_name = 'SFLIGHT' CHANGING it_outtab = me->gt_data. ENDMETHOD.</code>
Also Check – Just 4 Versions of the same program to understand OOPs ABAP
- Method: TOOLBAR
In this method, we will create one Push-Button (just as an example-no functionality) for example to save a record, in case we had created this ALV as editable.
<code>"Method Tool-bar METHOD toolbar. DATA: lv_toolbar TYPE stb_button. * Push Button "For Example SAVE CLEAR lv_toolbar. MOVE 'FC_SAVE' TO lv_toolbar-function. "Function Code lv_toolbar-icon = icon_system_save. "Save Icon MOVE 'Save'(100) TO lv_toolbar-text. "Push button Text MOVE 'Save'(100) TO lv_toolbar-quickinfo. "Push button Quick-Info MOVE space TO lv_toolbar-disabled. "Push button enabled APPEND lv_toolbar TO e_object->mt_toolbar. ENDMETHOD. </code>
- Method: USER_COMMAND
This method will be used to handle all the commands given by the user, for example if user press on the Save button we created above etc. I have kept this section as blank, you can add the logic as required.
<code>"Method User_command METHOD user_command. IF e_ucomm = ' '. ENDIF. ENDMETHOD. "USER_COMMAND ENDCLASS. "lcl_report IMPLEMENTATION</code>
In case you set the break-point at above method and press the Save button on the ALV, the execution will stop. Please check the below screenshot:

Finally, the Events of the program execution
<code>INITIALIZATION. * object for the report CREATE OBJECT lo_report. * generate output lo_report->generate_output( ). START-OF-SELECTION. * Get data lo_report->get_data( ).</code>
Conclusion
In this blog post we learnt the basics of creating an ALV on the selection screen with the usage of CL_GUI_ALV_GRID class.
If you just copy the above code snippets and paste it to your SE38 editor and activate it. Just like one of our lazy editors at SAPYard did. You will find the output like below. Yes!! It is a working code. And you do not need to design any screen elements.

Why did we need to use ABAP Memory when all data is there in the program and we are displaying them in the same program?
When the program control reaches INITIALIZATION event, all memory is REFRESHED and global data is made FREE. After all everything should be initialized in the INITIALIZATION event. That is the job of INITIALIZATION event. Therefore, we have to have the ALV data somewhere to pull it back when we are in INITIALIZATION event. Now you know why we were forced to take help of the ABAP Memory.
COMMENTS & QUESTIONS PLEASE!!
Do join 6060+ SAP Technical Consultants in this Telegram SAP Technical Discuss Group. Ask, Answer and Learn is our Motto. You need to install Telegram App first in your mobile or desktop and then click the joining link.
Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.
Please follow our LinkedIn Page, LinkedIn Group, Facebook Page, Twitter and Instagram.
Save our number +1-646-727-9273 and send us a Whatsapp message ‘LEARN’ to be part of our Learning Community.
Some more Tweaks & Tips on ALV
- How to Show the ALV Output and the Selection Screen on the same Screen?
- Selective Handling of the Buttons in ALV Grid Toolbar
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- Extensive Tips and Tricks for Interactive SAP ALV
- ALV with an Editable Row
- OOPs Report Using Splitter and ALV Tree Combination
- Know Who is doing What in your SAP System
- Just a key and two clicks for ALV consistency check
- Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’?
- Is data element WDY_BOOLEAN and Flag (Char1) same for Web Dynpro ALV?