Techskills

August 5, 2008

Learning SAP Basic — How to Write a Function Module

Filed under: Learn SAP, SAP — Tags: , , , — kumar @ 11:05 am

If you click on the source code (where you need to write the code) you can see all the input and output and exceptions that are prepopulated for this in the comment section. This will give you a idea of all the inputs and outputs and all other parameter for this function with out going to each place to look for the information. Once you added the code to the function module save the information and activate the object to avaliable for the SAP system to run the function.

Look at the foillowing code that I wrote. In the below example if there is any error then the system will return not a number. Let us run this below sample one with valid value and another with error and see the output.

  FUNCTION Z_TEST_FM.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     REFERENCE(IN_VAL2) TYPE  NUMC4
*”     REFERENCE(IN_VAL1) TYPE  NUMC4
*”  EXPORTING
*”     REFERENCE(OUT_VAL1) TYPE  NUMC4
*”  EXCEPTIONS
*”      NOT_A_NUMBER
*”———————————————————————-
 

  OUT_VAL1 = IN_VAL1 + IN_VAL2.
  if sy-subrc <> 0.
    raise not_a_number.
  endif.

ENDFUNCTION.

Let us run the function moduel and see the following screen shot for the results.

 

Now you can write a function module. All you need to do is use this function module in the program( report) or in class module. In the next post let us see how we can trun this above function module in to a RFC(Remote Function Call). 

 

 

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress