Background Processing for ALV Reports
Background Processing for ALV Reports 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.
|
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.
|
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 . This can be modified as per your requirement and convenience.
The F_GETDATA subroutine will now look like this.
|
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
|
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.