I had an interesting last week and what looked easy at the outset did my head in for quite a while. I tried various methods and the below approach was able to give me a satisfactory solution.
Let's have a look at the high level requirement.
- Copy a standard SAP transaction and customise it . Below picture describes the requirement
The transaction is started with only few fields of the complete selection screen as the selection screen has too many fields and may confuse the users.
The users need to navigate back and forth between different modes of output screen. At the first display of output screen ( let's call them as levels - so the output screen is at level 0 when initially displayed and if called again goes to level 1 and so on . Similarly when the user comes back from a higher level of screen, the level will decrease : from 1 to 0 ). And of course when the program navigates back from level 0 of selection screen, it should display the selection screen.
I prototyped using a simple program using flight model.
- Selection Screen : Contains all fields.
However, the transaction is always run with a variant which hides the last field.
- Let's test with some data.
We get the level 0 output screen.
Now click on filter ( search icon on top right )
and we get level 1 screen.
Looks good so far. Now, let's try going back - going back to level 0 gives the screen as anticipated. However, when we go back and see that the selection screen parameters have gone back. The selection screen has gone blank !
Let's see what's going on.
As we need to keep track of different levels of screen, if the level of screen is greater than 0.
.......
ELSEIF sy-ucomm = 'EXIT'.
IF gv_list_level > 0.
gv_list_level = gv_list_level - 1.
gt_flight[] = gt_master_flight[].
CALL SCREEN 100.
ENDIF.
When we want to go back to selection screen from screen at level 0, we use below:
SUBMIT zsubmit_flight
WITH report EQ 'ZTESTFLIGHT'
WITH variant = 'ZFLIGHT_VAR'
WITH SELECTION-TABLE gt_seltab .
zsubmit_flight is a standard SAP report used by the report and can't be changed by us.
SUBMIT (REPORT) WITH VARIANT = VARIANT
USING SELECTION-SET VARIANT
VIA SELECTION-SCREEN
Workaround:
1) Store selected values by call of RS_REFRESH_FROM_SELECTOPTIONS
2) Export the selection table before doing a program restart.
EXPORT gt_seltab TO MEMORY ID gc_sel_mem.
3) Retrieve the selection table AT SELECTION-SCREEN OUTPUT.
RS_VARIANT_CONTENTS gives the parameters and select-options actually visible in the variant.
IMPORT gt_seltab FROM MEMORY ID gc_sel_mem.
IF NOT gt_seltab[] IS INITIAL.
CALL FUNCTION 'RS_VARIANT_CONTENTS'
EXPORTING
report = 'ZTESTFLIGHT'
variant = 'ZFLIGHT_VAR'
TABLES
l_params = lt_params
l_selop = lt_selops
valutab = lt_value
EXCEPTIONS
variant_non_existent = 1
variant_obsolete = 2
OTHERS = 3.
IF sy-subrc <> 0.
clear:lt_value,
lt_selops,
lt_value.
ENDIF.
* Update parameters values
LOOP AT lt_params INTO lw_param.
READ TABLE gt_seltab REFERENCE INTO lo_values WITH KEY selname = lw_param-name.
IF sy-subrc = 0.
lv_attr = lo_values->selname.
TRANSLATE lv_attr TO UPPER CASE.
ASSIGN (lv_attr) TO <fs_attr_val>.
<fs_attr_val> = lo_values->low.
ENDIF.
ENDLOOP.
* Update select-option values
LOOP AT lt_selops INTO lw_param.
READ TABLE gt_seltab REFERENCE INTO lo_values WITH KEY selname = lw_param-name.
IF sy-subrc = 0.
CONCATENATE lo_values->selname 'SIGN' INTO lv_attr SEPARATED BY '-'.
TRANSLATE lv_attr TO UPPER CASE.
ASSIGN (lv_attr) TO <fs_attr_sign>.
<fs_attr_sign> = lo_values->sign.
CONCATENATE lo_values->selname 'OPTION' INTO lv_attr SEPARATED BY '-'.
TRANSLATE lv_attr TO UPPER CASE.
ASSIGN (lv_attr) TO <fs_attr_option>.
<fs_attr_option> = lo_values->option.
CONCATENATE lo_values->selname 'LOW' INTO lv_attr SEPARATED BY '-'.
TRANSLATE lv_attr TO UPPER CASE.
ASSIGN (lv_attr) TO <fs_attr_low>.
<fs_attr_low> = lo_values->low.
CONCATENATE lo_values->selname 'HIGH' INTO lv_attr SEPARATED BY '-'.
TRANSLATE lv_attr TO UPPER CASE.
ASSIGN (lv_attr) TO <fs_attr_high>.
<fs_attr_high> = lo_values->high.
lv_attr = lo_values->selname.
TRANSLATE lv_attr TO UPPER CASE.
ASSIGN (lv_attr) TO <fs_attr_main>.
CONCATENATE lv_attr '[]' INTO lv_attr_tab.
ASSIGN (lv_attr_tab) TO <fs_attr_tab>.
IF lo_values->low IS NOT INITIAL OR lo_values->high IS NOT INITIAL.
REFRESH <fs_attr_tab>.
APPEND <fs_attr_main> TO <fs_attr_tab>.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
REFRESH gt_seltab.
EXPORT gt_seltab TO MEMORY ID gc_sel_mem.
- Create a transaction 'ZFLIGHT' with program ZTESTFLIGHT , variant ZFLIGHT_VAR.
The code can be referred here:
ZSUBMIT_FLIGHT
https://github.com/viksingh/ABAP_Demo/blob/master/ZSUBMIT_FLIGHT
Program ZTESTFLIGHT with issues:
https://github.com/viksingh/ABAP_Demo/blob/master/ZTESTFLIGHT_1
Program ZTESTFLIGHT with corrections:
ABAP_Demo/ZTESTFLIGHT_2 at master · viksingh/ABAP_Demo · GitHub
The key here is function module RS_VARIANT_CONTENTS and the dynamic update of selection screen after restart of a transaction.