ABAP Function Module Integration: Calling and Handling Exceptions in Program

 We have created a function module in our previous part, but for the real world scenario, We must call our function module in the program because Customer will ultimately run the program, he will not be going to SE37 to execute the function module.


Calling Function Module in the Program :-

  • Step 1 :- Go to ABAP Editor ( SE38 ).

  • Step 2 :- Create a executable program.

  • Step 3 :- Take two input parameters for the sum of two numbers.

  • Step 4 :- Click on Patter → Select the first radio button → write the name of your function module and press enter.



  • Step 5 :- Provide the importing and exporting parameters.



Code :-

***********************************************
*Start of Program
**********************
*Declaring two input parameters
PARAMETERS : p_input1 TYPE numc2.
PARAMETERS : p_input2 TYPE numc2.

***************
*Declaring output variable.
DATA : lv_output type numc3.

*********************
*Calling function Module
CALL FUNCTION 'ZAR_SUM_OF_TWO_NUMBERS'
  EXPORTING
    pinput1 = p_input1
    pinput2 = p_input2
 IMPORTING
   POUTPUT = lv_output.
  .
****************************
*Displaying the output
WRITE :/ 'The sum of two numbers :',
       / lv_output.
*********************
*End of Program
**************************************************

Execute the Code :-



  • Press F8.




Using the Exceptions tab of Function Module :-

Requirement :-

  • In case any of the input is 0, then we will raise a exception
  • Else, the sum part will work.

Solution :-

  • Step 1:- Open the Function Builder (SE37).

  • Step 2 :- Open the the create function module.

  • Step 3 :- Go to exceptions tab and provide the below given description.



  • Step 4 :- go to source code and write the logic.



  • Step 5 :- Activate the function module.

  • Step 6 :- Go back to SE38 and open the above used program in change mode.

  • Step 7 :- Again call the function module.



    Note :- Now, you can see, this time a exceptions tab is also added.

Code :-

***********************************************
*Start of Program
**********************
*Declaring two input parameters
PARAMETERS : p_input1 TYPE numc2.
PARAMETERS : p_input2 TYPE numc2.

***************
*Declaring output variable.
DATA : lv_output type numc3.

*********************
*Calling function Module
CALL FUNCTION 'ZAR_SUM_OF_TWO_NUMBERS'
  EXPORTING
    pinput1        = p_input1
    pinput2        = p_input2
 IMPORTING
   POUTPUT        = lv_output
 EXCEPTIONS
   NOT_ZERO       = 1
   OTHERS         = 2
          .
IF sy-subrc <> 0.
	MESSAGE 'Inputs cannot be zero' type 'E'.
ENDIF.

****************************
*Displaying the output
WRITE :/ 'The sum of two numbers :',
       / lv_output.
*********************
*End of Program
**************************************************

Execute the program :-

  • Suppose, I only give only one input.



    • Press F8.


    • Here, SY-SUBCRC is not zero, therefore exceptions part is raised and given message is displayed.
  • Now, Suppose, I give both inputs.



    • Press F8.




We will see a real world complex example in the next part.

Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming