Once I needed to have several lists in MAIN window output and each of these lists needed to have different column headers. As usually, there are several ways how to achieve this and I’ll try to present two possible ways:
Using header window of type VAR
Window of type VAR is printed on each page of SAPScript so you can use the code inside to determine which list is currently on output and print the headers according to it. Let’s imagine you have 3 types of lists:
- Material with info about its UOM, Quantity and Price
- Approval list (imagine you need to have more aprovers)
- Change log
/: CASE &LIST_TYPE&
/: WHEN '1'
* Material,,UOM,,Quantity,,Price
/: WHEN '2'
* Aprover,,Signed,,Date,,Time
/: WHEN '3'
* Change date,,Version,,Description
/: ENDCASE
Because it’s not possible to pass variables between windows directly, you need to use a program where your global data will be stored during program runtime.
You can call this program to set or get the variables.
In the VAR window you need to read data to get current list type. So we need to modify the code above a bit:
/: DEFINE &LIST_TYPE& = ''
/: PERFORM GET_LIST_TYPE IN PROGRAM <XYZ>
/: CHANGING &LIST_TYPE&
/: END PERFORM
/*
* &ULINE(70)&
/: CASE &LIST_TYPE&
/: WHEN '1'
* Material,,UOM,,Quantity,,Price
/: WHEN '2'
* Aprover,,Signed,,Date,,Time
/: WHEN '3'
* Change date,,Version,,Description
/: ENDCASE
* &ULINE(70)&
The code above is now complete for the header window (type VAR). Now we need to prepare the MAIN window:
/* Set list type = 1
/: DEFINE &LIST_TYPE& = '1'
/: PERFORM SET_LIST_TYPE IN PROGRAM <XYZ>
/: USING &LIST_TYPE&
/: END PERFORM
/* ...
/* Printing the list of materials
/* ...
/* Set list type = 2
/: DEFINE &LIST_TYPE& = '2'
/: PERFORM SET_LIST_TYPE IN PROGRAM <XYZ>
/: USING &LIST_TYPE&
/: END PERFORM
/* The following NEW-PAGE command is important:
/* a forced pagebreak and new column headers for list of type 2
/* are printed in header window
/: NEW-PAGE
/* ...
/* Printing list of approvers
/* ...
/* Set list type = 3
/: DEFINE &LIST_TYPE& = '3'
/: PERFORM SET_LIST_TYPE IN PROGRAM <XYZ>
/: USING &LIST_TYPE&
/: END PERFORM
/: NEW-PAGE
/* ...
/* Printing change log
/* ...
In the code above you can see there§s always a definition of list type (1, 2, or 3) followed by the list contents itself ended by a NEW-PAGE command (after list 1 and two) so the start of the subsequent list is on separate page and the headers are printed correctly.
Now we have to code the program <XYZ> that will manage the “set” and “get” actions for list type:
* Declare global variable for list_type
DATA: g_list_type type i.
FORM get_list_type tab_in STRUCTURE itcsy
tab_out STRUCTURE itcsy.
FIELD-SYMBOLS: <fs_out> type any.
READ TABLE tab_out ASSIGNING <fs_out> INDEX 1.
IF sy-subrc = 0.
<fs_out>-value = g_list_type.
ENDIF.
ENDFORM.
FORM set_list_type tab_in STRUCTURE itcsy
tab_out STRUCTURE itcsy.
FIELD-SYMBOLS: <fs_in> type any.
READ TABLE tab_in ASSIGNING <fs_in> INDEX 1.
IF sy-subrc = 0.
CHECK <fs_in>-value = 1
OR <fs_in>-value = 2
OR <fs_in>-value = 3.
g_list_type = <fs_in>-value.
ENDIF.
ENDFORM.
Using a TOP section in MAIN window
This approach is a bit simpler and can be used without external program because everything is maintained within the MAIN window.
For printing the list headers we will use the TOP section of the MAIN window (TOP section is triggered on each page)
/: TOP
/* commands
/: ENDTOP
Before the TOP section is reached, we need to set the LIST_TYPE variable and fill it with initial value so the header is printed correctly on first page. In our scenario the TOP section can be like the following:
/: DEFINE &LIST_TYPE& = '1'
/*
/: TOP
* &ULINE(70)&
/: CASE &LIST_TYPE&
/: WHEN '1'
* Material,,UOM,,Quantity,,Price
/: WHEN '2'
* Aprover,,Signed,,Date,,Time
/: WHEN '3'
* Change date,,Version,,Description
/: ENDCASE
* &ULINE(70)&
/: ENDTOP
The following code in MAIN window will handle the rest of printing the data:
/* ...
/* printing the first list type data
/* ...
/*
/: DEFINE &LIST_TYPE& = '2'
/: NEW-PAGE
/* ...
/* printing the second list type data
/* ...
/*
/: DEFINE &LIST_TYPE& = '3'
/: NEW-PAGE
/* ...
/* printing the third list type data
/* ...