cancel
Showing results for 
Search instead for 
Did you mean: 

Subroutine in Sap script

Former Member
0 Kudos

How to declare a subroutine in sap script for retrieving data from database tables.N also how to call dose sub routines in Sap scripts windows.Please help me out.

Accepted Solutions (1)

Accepted Solutions (1)

former_member196280
Active Contributor
0 Kudos

WRITE THE FOLLOWING IN SAP SCRIPT

<b>/: PERFORM GET_PRICE IN PROGRAM <prog name>

/:USING &KOMVD-KBERT&

/:CHANGING &TOT_PRICE&

/:ENDPERFORM</b>

Goto SE38

create a program similar to <prog name>

<i><b>FORM GET_PRICE tables in_val structure itcsy

out_val structure itcsy.

data : value type komvd-kbert.

Read in_val table index 1.

"(or) READ TABLE in_val WITH KEY 'KOMVD-KBERT'

value = in_val-value.

Select <field names> into < variable names ex., value1> from komvd where kbert EQ value.

Read out_val table index 1.

out_val-value = value1.

Modify out_val index 1.

ENDFORM.</b></i>

Reward points to all useful answers.

Regards,

SaiRam

Answers (4)

Answers (4)

Former Member
0 Kudos

SAP Script is Text editor so you can not write the code ,so we use ITCSY Structure ..

try to write down in se71 (Text element) ..

/: PERFORM NAME( PERFORM NAME) IN PROGRAM PROG( SE38 PROGRAM)

/: USING &VAR1&

/: USING &VAR2&

/: USING &VARN&

/: CHANGING &OUTPUT1&

/: CHANGING &OUTPUT2&

/: CHANGING &OUTPUTN&

/: ENDPERFORM.

CREATE PROGRAM IN SE38 WHICH SAME AS IN SE71 (YOU MENTINED IN PERFORM.)

FORM NAME tables INT_INPUT STRUCTURE ITCSY

INT_OUTPUT STRUCTURE ITCSY .

ENDFORM

Former Member
0 Kudos

Hi friend,

You can call an ABAP subroutine from SapScript using the PERFORM statment. You can use this to get data without having to change the print program. In the example below NAME is retreived from table SCUSTOM.

SapScript

/:DEFINE &CUST& = '00000021'.

/:PERFORM GET_NAME IN PROGRAM Z_BC460_EX4_HF

/: USING &CUST&

/: CHANGING &NAME&

/:ENDPERFORM.

The ABAP routine

The ABAP routine could be defined as follows:

IMPORTANT: The structure itcsy must be used for the parameters.

REPORT Z_HENRIKF_SCRIPT_FORM .

TABLES scustom.

FORM get_name tables in_tab structure itcsy out_tab structure itcsy.

read table in_tab index 1.

select single * from scustom

where id = in_tab-value.

if sy-subrc = 0.

read table out_tab index 1.

move scustom-name to out_tab-value.

modify out_tab index sy-tabix.

else.

read table out_tab index 1.

move 'No name' to out_tab-value.

modify out_tab index sy-tabix.

endif.

    • You could also fill the ouput parameter table this way

  • READ TABLE out_par WITH KEY 'NAME1'.

  • out_par-value = l_name1.

  • MODIFY out_par INDEX sy-tabix.

ENDFORM.

Note that if you use more than one parameter you must use Using or Changing before every parameter !

/: PERFORM <form> IN PROGRAM <prog>

/: USING &INVAR1&

/: USING &INVAR2&

......

/: CHANGING &OUTVAR1&

/: CHANGING &OUTVAR2&

......

/: ENDPERFORM

dev_parbutteea
Active Contributor
0 Kudos

HI,

in sapscript , put codes like

/: PERFORM F_GET_PRICE IN PROGRAM <b><subroutine prog name></b>

/:USING &KOMVD-KBERT&

/:CHANGING &TOT_PRICE&

/:ENDPERFORM

using--> passes parameters from sapscript

changing--> retrieves values from routine

In a program in se38 , create a routine F_GET_PRICE

FORM F_GET_PRICE tables int_cond structure itcsy

outt_cond structure itcsy. data : value type kbert.

statics value1 type kbert.

Read int_cond table index 1.

value = int_cond-value.

<b>retrieve values from table</b>

Read outt_cond table index 1.

outt_cond-value = value1.

Modify outt_cond index 1.

ENDFORM.

regards,

Sooness.

former_member588853
Active Contributor
0 Kudos

HI,

You can use the PERFORM command to call an ABAP subroutine (form) from any program,

subject to the normal ABAP runtime authorization checking. You can use such calls to

subroutines for carrying out calculations, for obtaining data from the database that is needed at

display or print time, for formatting data, and so on.

PERFORM commands, like all control commands, are executed when a document is formatted

for display or printing. Communication between a subroutine that you call and the document is

by way of symbols whose values are set in the subroutine.

Syntax in a form window:

/: PERFORM <form> IN PROGRAM <prog>

/: USING &INVAR1&

/: USING &INVAR2&

......

/: CHANGING &OUTVAR1&

/: CHANGING &OUTVAR2&

......

/: ENDPERFORM

INVAR1

and INVAR2 are variable symbols and may be of any of the four SAPscript symbol

types.

OUTVAR1

and OUTVAR2 are local text symbols and must therefore be character strings.

The ABAP subroutine called via the command line stated above must be defined in the ABAP

report prog as follows:

FORM <form> TABLES IN_TAB STRUCTURE ITCSY

OUT_TAB STRUCTURE ITCSY.

...

ENDFORM.

The values of the SAPscript symbols passed with /: USING... are now stored in the internal

table IN_TAB . Note that the system passes the values as character string to the subroutine,

since the field Feld VALUE in structure ITCSY has the domain TDSYMVALUE (CHAR 80). See

the example below on how to access the variables.

The internal table OUT_TAB contains names and values of the CHANGING parameters in the

PERFORM statement. These parameters are local text symbols, that is, character fields

chk this sample code of driver program:

REPORT ZVKKSCRIPTS1 .

data: v_mat like mara-matnr,

var1 like makt-maktx.

form subroutine tables itab structure itcsy

otab structure itcsy.

read table itab with key name = 'IT_VBAP-MATNR'.

if sy-subrc = 0.

v_mat = itab-value.

select single maktx from makt into var1

where matnr = v_mat and

spras = sy-langu.

if sy-subrc = 0.

read table otab with key name = 'VAR1'.

if sy-subrc = 0.

otab-value = var1.

modify otab index sy-tabix.

endif.

endif.

endif.

endform.

rewards if useful

regards,

nazeer