Calling a subroutine from SAP SCRIPT form .

In order to call a subroutine program from a SAP Script you have to do 2 things :

1 – write your subroutine which will take values from the script and doing some calculations on them or use them to retrieve another set of values.

2- Calling the subroutine from the SAP Script in order to Pass some values and get newly calculated one.

Here’s an overview to let you know how to write a subroutine and how to call it from your Script …. Enjoy!

** First write your subroutine **

1-      Go to se38

2-      Create a program with type “ subroutine pool “

3-      Add your form in  the subroutine program :

Form (Form_Name) TABLES in_tab  STRUCTURE  itcsy

out_tab STRUCTURE  itcsy.


4- Read Values passed from the script to the Subroutine by writing the following code :

READ TABLE  in_tab  WITH KEY  ‘ Type your value name here ‘. “Read Data into internal table (in_tab)

CHECK sy-subrc = 0. “Check if  the record was read successfully.

(your variable name) = in_tab-value. “Take the value from the internal table into a variable

5- Passing Newly calculated values back to the Script by writing the following code :

READ TABLE out_tab WITH KEY ‘ Type your output value in CAPITAL LETTERS ONLY HERE‘. ” Output Parameter”

CHECK sy-subrc = 0. Check if the data was read successfully “

out_tab-value = Your Calculated Value Parameter .

MODIFY out_tab INDEX sy-tabix . ” Passing the values again to the sap script to be displayed

Here’s a demonstrating example to show you how to use the previous code :

Form Calc TABLES in_tab  STRUCTURE  itcsy

out_tab STRUCTURE  itcsy.

READ TABLE  in_tab  WITH KEY  ‘KOMVD_KWERT‘. “Input Parameter”

CHECK sy-subrc = 0.

KOMVD_KWERT_VAR= in_tab-value.

KOMVD_KWERT_VAR = KOMVD_KWERT_VAR  *  1000 / 93889. ” Any desired calculations

READ TABLE out_tab WITH KEY ‘RESULT.” Output Parameter”

CHECK sy-subrc = 0. ” If sy-subrc = 0 then data was read successfully

out_tab-value = KOMVD_KWERT_VAR

MODIFY out_tab INDEX sy-tabix .



** Second calling the Subroutine from the SAP Script **

by opening your SAP Script, right click on the main window then choose edit text and add the following code :

/: PERFORM Form_Name in Subroutine_Name




Example :

/:     PERFORM Calc IN PROGRAM  Z_Test




or you can define  your parameters first instead of using its technical names directly as follows:

/E    Calc_Line  ” Code Name

/:     DEFINE &INPUT& = &KOMVD-KWERT& “Passing Value from the script to the input parameter in the  program

/:     DEFINE &RESULT& ” Output Parameter key

/:     PERFORM Calc IN PROGRAM (Prog_Name)

/:      USING &INPUT& “Input Parameter”

/:      CHANGING &RESULT& ” Output Parameter”