Monday, May 18, 2009

Passing table data to the layout without changing the driver program

By Joyjit Ghosh

I have seen a typical requirement from client that SAP script layout need to be changed (additional data need to be displayed) without modifying the driver program (mainly standard SAP program). This tip will show us how to pass table data (multiple records at a time) to layout without changing the driver program.

Step1. Create a standard text from SO10.

Create a blank standard text. This will store the table data

Step2. Create a subroutine pool and a routine in it that can be called from SAP script.

From transaction SE38 create a subroutine pool.

Now create subroutine with proper interface to fetch the data from the table.

Step3. Within this routine write the logic to fetch the table data and populate the standard text.

***************************************************************
* Fetch table data and upload the data in proper format to the
* standard text
***************************************************************
DATA: i_zemployee TYPE STANDARD TABLE OF zemployee INITIAL SIZE 0,
w_zemployee TYPE zemployee,
i_text TYPE STANDARD TABLE OF tline INITIAL SIZE 0,
w_header LIKE thead,
w_text TYPE tline.
CONSTANTS: c_par TYPE char2 VALUE ',,'. " Sign for tabs
* Fetch data for employee
SELECT * FROM zemployee
INTO TABLE i_zemployee.
  IF sy-subrc = 0.
*  Create text table
LOOP AT i_zemployee INTO w_zemployee.
*     Store default paragraph format
w_text-tdformat = '*'.
*     Add all the required fields separated by tab
CONCATENATE w_zemployee-empno w_zemployee-empname
INTO w_text-tdline
SEPARATED BY c_par.
*     Store table data
APPEND w_text TO i_text.
    ENDLOOP.
    check sy-subrc = 0.
*   Populate header info
*   Text object
w_header-tdobject = 'TEXT'.
*   Standard text name
w_header-tdname = 'Z_TABLE_DATA'.
*   Text id
w_header-tdid = 'ST'.
*   Language
w_header-tdspras = 'E'.
*  Populate the standard text with table data
CALL FUNCTION 'SAVE_TEXT'
EXPORTING
header = w_header
insert = 'X'
savemode_direct = 'X'
TABLES
lines = i_text
EXCEPTIONS
id = 1
language = 2
name = 3
object = 4
OTHERS = 5
.
IF sy-subrc <> 0.
    ENDIF.
  ENDIF. 

Step4. Call the routine and standard text from the SAP script layout.

Note: For the sake of simplicity this tip is shown in a custom layout that is called from a custom report.

/*   Call the routine
/: PERFORM FETCH_TABLE_DATA IN PROGRAM Z_SUBROUTINE_POOL
/: USING &INVAR1&
/: CHANGING &OUTVAR1&
/: ENDPERFORM
/*   Now call the standard text
/: INCLUDE Z_TABLE_DATA OBJECT TEXT ID ST LANGUAGE EN

Step5. Test the SAP script form

Activate the SAP script debugger


Run the custom report to test the SAPscript layout.

*&---------------------------------------------------------------*
*& Report Z_TEST_SAPCSRIPT *
*&---------------------------------------------------------------*
REPORT  Z_TEST_SAPCSRIPT                        .
start-of-selection.
CALL FUNCTION 'OPEN_FORM'
EXPORTING
DEVICE = 'PRINTER'
DIALOG = 'X'
FORM = 'Z_DEMO_LAYOUT'
LANGUAGE = SY-LANGU
EXCEPTIONS
CANCELED = 1
DEVICE = 2
FORM = 3
OPTIONS = 4
UNCLOSED = 5
MAIL_OPTIONS = 6
ARCHIVE_ERROR = 7
INVALID_FAX_NUMBER = 8
MORE_PARAMS_NEEDED_IN_BATCH = 9
SPOOL_ERROR = 10
CODEPAGE = 11
OTHERS = 12
.
IF sy-subrc = 0.
CALL FUNCTION 'WRITE_FORM'
EXPORTING
ELEMENT = '001'
FUNCTION = 'SET'
TYPE = 'BODY'
WINDOW = 'MAIN'
* IMPORTING
* PENDING_LINES =
EXCEPTIONS
ELEMENT = 1
FUNCTION = 2
TYPE = 3
UNOPENED = 4
UNSTARTED = 5
WINDOW = 6
BAD_PAGEFORMAT_FOR_PRINT = 7
SPOOL_ERROR = 8
CODEPAGE = 9
OTHERS = 10
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 CALL FUNCTION 'CLOSE_FORM'
EXCEPTIONS
UNOPENED = 1
BAD_PAGEFORMAT_FOR_PRINT = 2
SEND_ERROR = 3
SPOOL_ERROR = 4
CODEPAGE = 5
OTHERS = 6
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.

After execution of this report SAPscript debugger is triggered.

As shown below layout is calling the code written in the routine

Standard text is populated with the data fetched from table

Output of SAP script:

Table entries:

No comments: