Function Modules are sub-programs which contains set of re-usable statements with importing, exporting parameters, exceptions. Unlike include programs Function Modules can be executed independently.
Import: These are input parameters of a Function Module.
Export: These are output parameters of a Function Module.
Changing: These are parameters which act as importing and exporting parameters to a Function Module.
Tables: These are internal tables which also acts as importing and exporting parameters.
Exceptions: Exceptions in Function Modules are used to catch certain type of errors.
In this lesson we are going to learn how to create Function Group and Function Module, in order to create a Function Module we need a Function Group (We can save in existing one also), follow the below steps to create a Function Group.
Go to SE80.
Select 'Function Group' from drop-down list.
Provide a Function Group name ex: ZTFUNCTIONGROUP and press enter.
A Popup will open Click on 'Yes' and provide short text, click on save.
Select Local Object.
Right-Click on Function Group name (ZTFUNCTIONGROUP) and activate.
Creating Function Module:
Here we take a small requirement and create a Function Module.
Requirement: Develop a Function Module, which displays Material details for a material no.
Go to SE37, provide a Function Module name ZSAPN_GET_MATERIAL, click on create.
A pop up will open, provide a Function Group name ZTEST_SAPNUTS and short text, Save
An information message will come saying that Function Module name is reserved for SAP
Add below parameters.
Import: Select Import tab and enter
IM_MATNR under 'Parameter name',
TYPE under typing and
MARA-MATNR under 'Associated Type', enter. See below image
Export: Select Export tab and enter
EX_MARA under 'Parameter name',
TYPE under typing and
MARA under Associated Type, enter. See below image
Select Source Code tab and write below code to get material details, save as local object and activate.
SELECT SINGLE * FROM MARA INTO EX_MARA WHERE MATNR = IM_MATNR.
Test Function Module.
Click on Execute icon (F8), and give a material no, execute (F8).
It will get material details
Using Function Modules in ABAP Programs
CALL FUNCTION is a keyword which is used to call a Function Module in ABAP Programs, now we use the above created Function Module in our ABAP program.
Go to SE38 and Provide name as ZSAPN_GET_MATERIAL and click on Create.
Provide a title, select Executable Program from drop-down and Save as local object.
Declare work area and an input field (parameter).
:WA_MARA TYPE MARA ." workarea to store material details PARAMETERS :P_MATNR TYPE MARA-MATNR ."Material no input START-OF-SELECTION. "Default event
To Call a Function Module, click on 'Pattern'
A pop up will open, select CALL FUNCTION radio, provide Function Module name and click on continue icon (tick
The below code will be generated.
CALL FUNCTION 'ZSAPN_GET_MATERIAL' EXPORTING IM_MATNR = IMPORTING EX_MARA = IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF.
Now pass material no parameter and receive material data into a work area, full program code is below.
REPORT ZSAPN_GET_MATERIAL. DATA
:WA_MARA TYPE MARA .PARAMETERS :P_MATNR TYPE MARA-MATNR .START-OF-SELECTION. CALL FUNCTION 'ZSAPN_GET_MATERIAL' EXPORTING IM_MATNR = P_MATNR IMPORTING EX_MARA = WA_MARA .IF SY-SUBRC <> 0. * Implement suitable error handling here ENDIF. WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MBRSH . "Display result
Execute (F8) and test the program.