Subroutine in SAP Script
Let us go with the existing SAP script. We need to create a subroutine inside this script. As we know the usability of subroutine we want to create a subroutine inside this script. With the help of this subroutine we want to show the Bank details of existing vendor so LFBK table has been used. Now the actual coding part is taken care by SE38 where a subroutine pool program needs to be created. We are calling that program inside this form. To call this program we have to go to Text element with change editor. The steps are as follows.
Step 1: Go to text element change editor. The code can be written there by using '/:' operator.
Step 2: Now create a subroutine pool program in SE38.
Step 3: This program will contain only the form and endform. We can declare nested forms also. The program is as follows.
Step 4: Finally in the form we can see the Bank details.
Step 1: Go to text element change editor. The code can be written there by using '/:' operator.
Step 2: Now create a subroutine pool program in SE38.
Step 3: This program will contain only the form and endform. We can declare nested forms also. The program is as follows.
PROGRAM zsubroutine_script.
*&---------------------------------------------------------------------*
*& Form get_bank
*&---------------------------------------------------------------------*
* Subroutine by which getting data from vendor bank table LFBK
*----------------------------------------------------------------------*
FORM get_bank TABLES it_input STRUCTURE itcsy
it_output STRUCTURE itcsy.
*-Declaring input table and output table with structure ITCSY
*-ITCSY is a standard structure which holds two fields
*-Name & Value
*-Here the name is field name & value is field value
"Declaring bank details work area structure
DATA: BEGIN OF wa_lfbk,
lifnr TYPE lfbk-lifnr,
banks TYPE lfbk-banks,
bankl TYPE lfbk-bankl,
END OF wa_lfbk.
DATA: wa_var TYPE itcsy, "work area for ITCSY
lv_var TYPE lifnr. "variable for conversion
IF it_input IS NOT INITIAL.
"Reading the input table with key name
READ TABLE it_input INTO wa_var
WITH KEY name = 'WA_LFA1-LIFNR'.
IF sy-subrc = 0.
"Conversion for vendor account number
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_var-value
IMPORTING
output = lv_var.
"Selecting the bank details
SELECT SINGLE lifnr banks bankl
FROM lfbk INTO wa_lfbk
WHERE lifnr = lv_var.
IF sy-subrc = 0.
"Reading the output table with key name - first field
READ TABLE it_output INTO wa_var
WITH KEY name = 'WA_LFBK-BANKS'.
IF sy-subrc = 0.
wa_var-value = wa_lfbk-banks. "Bank country
"Modifying the output table with values - bank country
MODIFY it_output FROM wa_var INDEX sy-tabix.
ENDIF.
"Reading the output table with key name - second field
READ TABLE it_output INTO wa_var
WITH KEY name = 'WA_LFBK-BANKL'.
IF sy-subrc = 0.
wa_var-value = wa_lfbk-bankl. "Bank key
"Modifying the output table with values - bank key
MODIFY it_output FROM wa_var INDEX sy-tabix.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. "get_bank
*&---------------------------------------------------------------------*
*& Form get_bank
*&---------------------------------------------------------------------*
* Subroutine by which getting data from vendor bank table LFBK
*----------------------------------------------------------------------*
FORM get_bank TABLES it_input STRUCTURE itcsy
it_output STRUCTURE itcsy.
*-Declaring input table and output table with structure ITCSY
*-ITCSY is a standard structure which holds two fields
*-Name & Value
*-Here the name is field name & value is field value
"Declaring bank details work area structure
DATA: BEGIN OF wa_lfbk,
lifnr TYPE lfbk-lifnr,
banks TYPE lfbk-banks,
bankl TYPE lfbk-bankl,
END OF wa_lfbk.
DATA: wa_var TYPE itcsy, "work area for ITCSY
lv_var TYPE lifnr. "variable for conversion
IF it_input IS NOT INITIAL.
"Reading the input table with key name
READ TABLE it_input INTO wa_var
WITH KEY name = 'WA_LFA1-LIFNR'.
IF sy-subrc = 0.
"Conversion for vendor account number
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_var-value
IMPORTING
output = lv_var.
"Selecting the bank details
SELECT SINGLE lifnr banks bankl
FROM lfbk INTO wa_lfbk
WHERE lifnr = lv_var.
IF sy-subrc = 0.
"Reading the output table with key name - first field
READ TABLE it_output INTO wa_var
WITH KEY name = 'WA_LFBK-BANKS'.
IF sy-subrc = 0.
wa_var-value = wa_lfbk-banks. "Bank country
"Modifying the output table with values - bank country
MODIFY it_output FROM wa_var INDEX sy-tabix.
ENDIF.
"Reading the output table with key name - second field
READ TABLE it_output INTO wa_var
WITH KEY name = 'WA_LFBK-BANKL'.
IF sy-subrc = 0.
wa_var-value = wa_lfbk-bankl. "Bank key
"Modifying the output table with values - bank key
MODIFY it_output FROM wa_var INDEX sy-tabix.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. "get_bank
Step 4: Finally in the form we can see the Bank details.