Applies to
All SAP versions which support ALVs
Summary
This technique allows the user to process ALV reports in background and then display the results in foreground
Author(s): Kris Donald
Company: L&T Infotech
Created on: 14 March 2013
Author Bio
Kris Donald is an SAP technical consultant working for L&T Infotech, Mumbai, India. He has worked in SAP since September 2006 and his experience spans various technologies primarily ABAP
Requirement
ALV reports are usually run in foreground. When the data selection and processing is large, this can lead to time outs. Execution in background does not allow further processing of the ALV output such as hotspots / custom buttons etc.
Solution
When the report is run in background mode, store the results into a database table (such as INDX) which can be retrieved later.
Base Report
First, create an ALV report (eg: ZTEST_MAIN) which displays the ALV using the standard function module REUSE_ALV_GRID_DISPLAY or class CL_GUI_ALV_GRID.
A usual ALV report would have three sections
- Fetching the data / processing it
- Displaying the ALV
- Handling user interaction
Here, we will use a simple report which fetches material data from table MARA and displays it
It would look something like this.
|
*&———————————————————————* *& Report ZTEST_MAIN *& *&———————————————————————* *& Author : Kris Donald *& Date : 14-03-2013 *& Purpose : Test program for ALV processing *&———————————————————————*
report ztest_main.
data: gv_repid like sy-repid value ‘ZTEST_MAIN’. constants: c_x type xfeld value ‘X’.
selection-screen begin of block b1 with frame title f1. parameters: p_count type i default ‘200’. “ Number of rows to select parameters: p_msg type c length 100 lower case default ‘Test message from screen’. “ Message selection-screen end of block b1.
initialization. f1 = ‘Parameters’.
start-of-selection.
data: it_mara type table of mara. data: wa_mara like line of it_mara.
* get the data perform f_getdata.
* ALV display perform f_output.
*&———————————————————————* *& Form f_getdata *&———————————————————————* * Get the data either by db selection *———————————————————————-* form f_getdata.
* data selection select * from mara into table it_mara up to p_count rows.
* data processing loop at it_mara into wa_mara. wa_mara-aenam = sy-uname. modify it_mara from wa_mara. endloop.
* adding an artificial wait to simulate further complex processing wait up to 3 seconds.
endform. “f_getdata
*&———————————————————————* *& Form f_output *&———————————————————————* * Display the output in the ALV *———————————————————————-* form f_output.
* display the ALV grid when we are in foreground mode call function ‘REUSE_ALV_GRID_DISPLAY’ exporting i_callback_program = gv_repid “ i_callback_pf_status_set = ‘F_PF_STATUS’ i_callback_user_command = ‘F_USER_COMMAND’ i_structure_name = ‘MARA’ tables t_outtab = it_mara exceptions program_error = 1 others = 2 . if sy-subrc ne 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. endif.
endform. “f_output
*&———————————————————————* *& Form f_pf_status *&———————————————————————* * subroutine to set the pf-status *———————————————————————-* form f_pf_status using rt_extab type slis_t_extab.
set pf-status ‘ZSALV_STANDARD’ excluding rt_extab.
endform. “f_pf_status
*&———————————————————————* *& Form F_USER_COMMAND *&———————————————————————* * contains processing when buttons are clicked *———————————————————————-* * –>R_UCOMM – User command * –>RS_SELFIELD – Details of what is selected *———————————————————————-* form f_user_command using r_ucomm like sy-ucomm rs_selfield type slis_selfield.
case r_ucomm.
when ‘MYFUNCTION’. ” show the parameter from the screen message p_msg type ‘I’.
endcase. ” r_ucomm
endform. “f_user_command
|
|
|
GUI Status
Copy the GUI_STATUS SALV_STANDARD from program SALV_DEMO_HIERSEQ_EVENTS into your report or create any GUI status which has a custom button and assign function code MYFUNCTION to that button.
Call the GUI status in your report by using the appropriate logic.
Note: This step is not mandatory, but it will demonstrate the fact that we can use the values in the original selection screen when we finally display the ALV
Selection Screen Button
Create a button on the selection screen as described below which will allow the user to check and display the output of the background job.
1. Global declaration
tables sscrfields.
type-pools: slis,icon.
data: wa_functxt type smp_dyntxt.
2. Selection screen declaration
selection-screen function key 1. “Will have a function code of ‘FC01’
3. Initialization code to populate the new button
Initialization.
* initialize display from memory button
wa_functxt-icon_id = icon_background_job.
wa_functxt-quickinfo = ‘Execute report from stored job’.
wa_functxt-icon_text = ‘Disp. stored job’.
sscrfields-functxt_01 = wa_functxt.
4. Call a new report on click of the button
at selection-screen.
* Handle user commands on the selection screen
case sscrfields-ucomm.
when ‘FC01’.
submit ztest_stored via selection-screen and return.
when others.
endcase.
We will come back to the contents of report ZTEST_STORED later.
The Toolbar will now look like this

Modifications in the Base Report
1. Selection Screen
We will add two hidden fields to the selection screen.
- P_IMPORT – When this is set, it indicates that the program was called from ZTEST_STORED and should import the data directly from the INDX table instead of the original selection / processing logic
- P_KPDATA – When this is set, it indicates that the program should not delete the INDX table data, keeping it for later use.
|
* parameter will be set when the program is called from another program parameter: p_import type xflag no-display.
* parameter indicates that indx data should be kept for later parameter: p_kpdata type xflag no-display. ” do not delete cluster
|
|
2. Reading the data
If the flag P_IMPORT is set, then we will read the data from the INDX table directly, else we will run the pre-existing logic.
A global constant is also declared which is used in generating the memory id for the import.
constants: gc_id type c length 8 value ‘Z123TEST’.
The format used for generating the key is <gc_id><counter><sy-uname>. This can be modified as per your requirement and convenience.
The F_GETDATA subroutine will now look like this.
|
*&——————————————————————* *& Form f_getdata *&——————————————————————* * Get the data either by db selection, or fetching from INDX *——————————————————————-* form f_getdata.
data: lv_memid type indx-srtfd.
if p_import eq c_x.
* import main table concatenate gc_id ’01’ sy-uname into lv_memid. import tab = it_mara from database indx(xy) to wa_indx client sy-mandt id lv_memid.
if sy-subrc eq 0 and p_kpdata eq space. * deletes the data to save wastage of memory delete from database indx(xy) client sy-mandt id lv_memid. endif.
else.
* data selection select * from mara into table it_mara up to p_count rows.
* data processing loop at it_mara into wa_mara. wa_mara-aenam = sy-uname. modify it_mara from wa_mara. endloop.
* adding an artificial wait to simulate further complex processing wait up to 3 seconds.
endif. ” p_import
endform. “f_getdata
|
|
|
3. Displaying the ALV
Here, we can check the system variable SY-BATCH.
If we are in the foreground mode, then this variable will not be set and we can display the ALV as usual.
In background mode, this variable will be set. We then export the current table as well as the selection screen to memory.
Note that several programs rely on the data in the selection screen for further processing. This is the reason for exporting the selection screen as well.
For each internal table which we need to export, we can do so incrementing the counter when we populate the memory id field.
The F_OUTPUT subroutine will now look like this
|
*&——————————————————————* *& Form f_output *&——————————————————————* * Either display the output in the ALV or store it *——————————————————————-* form f_output.
if sy-batch eq c_x. * when we process the report in background mode, then do not dislay the alv * instead, export the data to INDX
data: lv_memid type indx-srtfd.
* get the current parameters data: li_selectionscreen type table of rsparams.
call function ‘RS_REFRESH_FROM_SELECTOPTIONS’ exporting curr_report = gv_repid * IMPORTING * SP = tables selection_table = li_selectionscreen exceptions not_found = 1 no_report = 2 others = 3 . if sy-subrc ne 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. endif.
* export main data concatenate gc_id ’01’ sy-uname into lv_memid. * delete old data delete from database indx(xy) client sy-mandt id lv_memid. * export new data export tab = it_mara to database indx(xy) from wa_indx client sy-mandt id lv_memid.
* export li_selectionscreen concatenate gc_id ’02’ sy-uname into lv_memid. * delete old data delete from database indx(xy) client sy-mandt id lv_memid. * export new data export tab = li_selectionscreen to database indx(xy) from wa_indx client sy-mandt id lv_memid.
message ‘Data exported to memory’ type ‘S’.
else.
* display the ALV grid when we are in foreground mode call function ‘REUSE_ALV_GRID_DISPLAY’ exporting i_callback_program = gv_repid i_callback_pf_status_set = ‘F_PF_STATUS’ i_callback_user_command = ‘F_USER_COMMAND’ i_structure_name = ‘MARA’ tables t_outtab = it_mara exceptions program_error = 1 others = 2 . if sy-subrc ne 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. endif.
endif. ” sy-batch
endform. “f_output
|
|
|
New Report ZTEST_STORED
Finally, create a new Z report which will get called when the user clicks on the ‘Disp. stored job’ button on the selection screen of the original report.
This program will check if data exists in memory for this report (for the current user) and if so, allow the user to execute the original report. Submitting the report will set the P_IMPORT flag on the original report.
When data does not exist, the selection screen will look like this

When data does exist, it will look like this

When the user enters the report, in initialization, we can check if data exists for any one internal table which we have exported. Accordingly, we can show the red / green status.
If the status is green, executing this report will submit the original report passing the same selections which the user originally selected.
The user has the option to keep the stored data for later use or to delete it from memory.
Below is the code for the report.
|
*&———————————————————————* *& Report ZTEST_STORED *&———————————————————————* *& Author : Kris Donald *& Date : 14-03-2013 *& Purpose : Allow user to execute stored jobs for ZTEST_MAIN *&———————————————————————* *& Date Changed by Tag Description *& *&———————————————————————*
report ztest_stored.
type-pools: icon.
selection-screen skip 1.
selection-screen comment /1(79) v_commt.
selection-screen begin of block b1 with frame title text-s01.
selection-screen skip 1. parameter: p_kpdata as checkbox. ” do not delete cluster selection-screen skip 1.
selection-screen end of block b1.
data: gv_memid type indx-srtfd. data: wa_indx type indx.
*——————————————————* * Initialization. *——————————————————* initialization.
* get the current parameters data: it_selectionscreen type table of rsparams. data: wa_selectionscreen type rsparams.
* import i_sales_order_display concatenate ‘Z123TEST02’ sy-uname into gv_memid. import tab = it_selectionscreen from database indx(xy) to wa_indx client sy-mandt id gv_memid. if sy-subrc eq 0. concatenate: icon_green_light space space ‘Data exists in memory’
into v_commt. else. concatenate: icon_red_light space space ‘Data does not exist in memory’
into v_commt. endif.
*——————————————————* * START-OF-SELECTION. *——————————————————* start-of-selection.
constants: c_x type xflag value ‘X’.
data: lv_tabix type sy-tabix.
if not it_selectionscreen[] is initial.
if p_kpdata eq space.
* deletes the data to save wastage of memory delete from database indx(xy) client sy-mandt id gv_memid.
endif.
* executes the main report with the original selection screen parameters submit ztest_main with selection-table it_selectionscreen with p_import = c_x with p_kpdata = p_kpdata.
else.
* Kindly run ZRSH in background mode first message ‘Kindly run ZTEST_MAIN in background mode first’ type ‘S’.
endif.
|
|
|
User Execution Steps
1. Run the original transaction and execute the report in background after entering the parameters


After this, the user can go to another transaction or even log off from SAP if needed and come back to the report selection screen at a later point of time.
2. One the report selection screen, click on the ‘Disp. stored job’ button

3. If the status is green, then click on the execute button

4. This will display the original ALV.
The user can then execute any of the foreground ALV functions such as custom buttons / hotspots.


VERY GOOD..... 🙂