Thats it. Now we will create a function module. Same SE37 transaction create a FM zau_getwerks.
Give function group name we just created and give a short text for the FM
Page 2 of 6
We are creating this FM which accepts a material number and gives the plant details for that material. Create an import parameter lv_matnr of type matnr. We can make it pass by value also by checking Pass by value. By default it is pass by reference only. If optional is checked then passing this parameter is not mandatory in the program.
Import Parameters - FM Here I will create a output table named output which is having a structure of zty_material. Create a zty_material structure from SE11 . Data type >> Create structure and add matnr and werks component to that structure.
Tables in FM We will add exceptions now. Exceptions provide a easy way for error handling in FMs. We will create 2 FMs One FM when we enter invalid material and another when data is not present related to plant.(No data in marc table)
Exceptions in FM Now the main coding/logic part of the program. Its a simple select statement. And we are checking sy-subrc to check whether we obtained data or not. If no data is fetched then we raise appropriate exceptions by using RAISE command.
Page 3 of 6
Source Code FM
FUNCTION zau_getwerks. *"---------------------------------------------------------------------*"*"Local Interface: *" IMPORTING *" REFERENCE(LV_MATNR) TYPE MATNR *" TABLES *" OUTPUT STRUCTURE ZTY_MATERIAL *" EXCEPTIONS *" NOMAT *" NODATA *"---------------------------------------------------------------------DATA: v_matnr TYPE matnr. SELECT matnr werks FROM marc INTO TABLE output WHERE matnr EQ lv_matnr. IF sy-subrc NE 0. SELECT single matnr FROM mara INTO v_matnr WHERE matnr EQ lv_matnr. IF sy-subrc NE 0. RAISE nomat. ELSE. RAISE nodata. ENDIF. ENDIF. ENDFUNCTION.
Activate the FM. Now activate the FM and test the FM by clicking executes. Pass a material number and see the output
Page 4 of 6
Test FM
Page 5 of 6
CALL FUNCTION 'ZAU_GETWERKS' EXPORTING lv_matnr = p_matnr TABLES output EXCEPTIONS nomat nodata = 1 = 2 = it_marc
OTHERS = 3. IF sy-subrc EQ 0. LOOP AT it_marc INTO wa_marc. WRITE:/5 wa_marc-matnr, wa_marc-werks. ENDLOOP. ELSEIF sy-subrc EQ 1. MESSAGE 'Invalid material' TYPE 'S' DISPLAY LIKE 'E'. ELSEIF sy-subrc EQ 2. MESSAGE 'No data present for plant' TYPE 'S' DISPLAY LIKE 'E'. ENDIF.
Thats it. Its as simple as that. Every exception is given numbers by default and we can check sy-subrc for these numbers to identify the exception.
Running Report
Report Output
Page 6 of 6