ABAP Subroutines in Script


ABAP Subroutines can be called from the sap script with the target program name that defines the source code of the perform statement. The PERFORM statement in the sap script window text element can use any number of USING & changing place holders. These are passed to the FORM & ENDFORM defined in the target program in IN_TAB and OUT_TAB table which get their fields from the structure ITCSY.

 The below post , reads a parameters from the driver program and passes it to the Script. Then the script PERFORM statement uses this parameter value and calls the subroutine in the target program to read information based on the input value.



Step1. So here we have the script main WINDOW. Select it and click on the Text Element.


Step2. Put the  below code where we have the PERFORM statement  with the target program name that defines the subroutine. Go back and activate the form.

Step3. Here we have the driver program which also acts a target program of the subroutine call. The target program of the subroutine called inside the script can be another program other than the driver program.



PARAMETERS : p_carrid TYPE spfli-carrid DEFAULT ‘LH’.

DATA: d_carrid TYPE spfli-carrid.


d_carrid = p_carrid.

CALL FUNCTION ‘OPEN_FORM’.


CALL FUNCTION ‘START_FORM’

 EXPORTING

  form    = ‘ZSCRIPT_WORK1’.


CALL FUNCTION ‘WRITE_FORM’

EXPORTING

   element    = ‘TXT_ELEM_1’

   “FUNCTION   = ‘SET’

   “TYPE       = ‘BODY’

   window     = ‘MAIN’.


CALL FUNCTION ‘END_FORM’.

CALL FUNCTION ‘CLOSE_FORM’.


FORM get_flight_details TABLES in_tab  STRUCTURE itcsy

                               out_tab STRUCTURE itcsy.

DATA: ls TYPE spfli.

IF in_tab IS NOT INITIAL.

  READ TABLE in_tab INDEX 1.

  IF in_tab-name = ‘D_CARRID’.

  SELECT SINGLE * FROM spfli INTO ls WHERE carrid = in_tab-value.

  ENDIF.

ENDIF.

IF ls IS NOT INITIAL.

 READ TABLE out_tab INDEX 1.

 IF sy-subrc = 0.

   out_tab-value = ls-carrid.

   MODIFY out_tab INDEX 1.

  ENDIF.

 READ TABLE out_tab INDEX 2.

 IF sy-subrc = 0.

    out_tab-value = ls-connid.

    MODIFY out_tab INDEX 2.

  ENDIF.

 READ TABLE out_tab INDEX 3.

 IF sy-subrc = 0.

    out_tab-value = ls-cityfrom.

    MODIFY out_tab INDEX 3.

  ENDIF.

 READ TABLE out_tab INDEX 4.

 IF sy-subrc = 0.

    out_tab-value = ls-cityto.

    MODIFY out_tab INDEX 4.

  ENDIF.

 READ TABLE out_tab INDEX 5.

 IF sy-subrc = 0.

    out_tab-value = ls-countryfr.

    MODIFY out_tab INDEX 5.

  ENDIF.

 READ TABLE out_tab INDEX 6.

 IF sy-subrc = 0.

    out_tab-value = ls-countryto.

    MODIFY out_tab INDEX 6.

  ENDIF.

 READ TABLE out_tab INDEX 7.

 IF sy-subrc = 0.

    out_tab-value = ls-airpfrom.

    MODIFY out_tab INDEX 7.

  ENDIF.

 READ TABLE out_tab INDEX 8.

 IF sy-subrc = 0.

    out_tab-value = ls-airpto.

    MODIFY out_tab INDEX 8.

  ENDIF.

ENDIF.

ENDFORM.


 

Step4. Execute the driver program.

Step5. So here we have the O/P.


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s