Thursday, September 22, 2016

Subroutines/Function Modules/RFC Function Module


Subroutine

A subroutine is a reusable section of code. It is a modularization unit within the program where a function is encapsulated in the form of source code. You page out a part of a program to a subroutine to get a better overview of the main program, and to use the corresponding sequence of statements many times as depicted in the following diagram.
Subroutine Reusable Section
We have program X with 3 different source code blocks. Each block has the same ABAP statements. Basically, they are the same code blocks. To make this code easier to maintain, we can encapsulate the code into a subroutine. We can call this subroutine in our programs as many times as we wish. A subroutine can be defined using Form and EndForm statements.
Following is an example of a subroutine:
START-OF-SELECTION.
   TRY.
       PERFORM sub_one.
     CATCH cx_...
       MESSAGE 'catch' TYPE 'I'.
   ENDTRY.

FORM sub_one USING .. CHANGING .. TABLES .. RAISING cx_.. .
 .......
 RAISE EXCEPTION TYPE cx_....
 .......
 ENDFORM.

The interface of ABAP subroutines supports USING, CHANGING, TABLES and RAISING parameters.
  • USING indicates parameters the subroutine can use for calculations
  • CHANGING indicates the value of the parameter can change after using the subroutine
  • TABLES indicates an internal table is to be used (internet says you shouldn't use it)
  • RAISING indicates the exception which is raised from the subroutine
We can call a subroutine by using PERFORM statement. The control jumps to the first executable statement in the subroutine <subroutine_name>. When ENDFORM is encountered, control jumps back to the statement following the PERFORM statement.
Example
Step 1 − Go to transaction SE80. Open the existing program and then right-click on program. In this case, it is 'ZSUBTEST'.
Step 2 − Select Create and then select Subroutine. Write the subroutine name in the field and then click the continue button. The subroutine name is 'Sub_Display' as shown in the following screenshot.
Create Subroutine
Step 3 − Write the code in FORM and ENDFORM statement block. The subroutine has been created successfully.
We need to include PERFORM statement to call the subroutine. Let’s take a look at the code −
REPORT ZSUBTEST.
PERFORM Sub_Display.

* Form Sub_Display
* -->  p1 text
* <--  p2 text

FORM Sub_Display.
Write: 'This is Subroutine'.
Write: / 'Subroutine created successfully'.
ENDFORM.                    " Sub_Display
Step 4 − Save, activate and execute the program. The above code produces the following output −
Subroutine Test: 
This is Subroutine
Subroutine created successfully
Hence, using subroutines makes your program more function-oriented. It splits the program's task into sub-functions, so that each subroutine is responsible for one subfunction. Your program becomes easier to maintain as changes to functions often only have to be implemented in the subroutine.

Function Modules

Function Modules are general purpose ABAP/4 routines that anyone can use. In fact , there are a large number of standard function Modules available.
Function Modules are organized into Function Groups: Collections of logically related functions. A Function module always belongs to a Function Group.
Syntax-
FUNCTION <function module>
<Statements>
ENDFUNCTION.
Important information Associated with Function Module
  • Administration
  • Import/Changing/Export parameters.
  • Table Parameters/Exceptions.
  • Documentation
  • Source code - L<fgrp>U01 . <fgrp> is the Function Group
  • Global Data - L<fgrp>TOP .Global data for the function group- Accessible across function modules in the function group.
  • Main Program (function pool) which can be displayed in SE38 - SAPL<fgrp> . Contains the list of all the include files for that function group
Call a Function Module
To call a function module, use the CALL FUNCTION statement:
CALL FUNCTION <module>

[EXPORTING  f1 = a 1.... f n = a n]

[IMPORTING  f1 = a 1.... f n = a n]

[CHANGING   f1 = a 1.... f n = a n]

[TABLES f1 = a 1.... f n = a n]

[EXCEPTIONS e1 = r 1.... e n = r n [ERROR_MESSAGE = r E]

[OTHERS = ro]].
Function Groups
Function groups are containers for function modules. Infact, there are a large number of standard Function Groups. All of the function modules in a function group can access the global data of the group.
Like executable programs (type 1) and module pools (type M), function groups can contain screens, selection screens, and lists.
Points to Note
  • Function Groups cannot be executed.
  • The name of a function group can be up to 26 characters long.
  • When you create a function group or function module, the main program and include programs are generated automatically.
  • Function groups encapsulate data.
How to create a Function Group
  1. Goto Transaction SE80.
  2. Select Program in the DropDown.
  3. Write the name of the Function Group That you want to create. Generally User made Function groups start with "Z". e.g. - <Z_FUNCTION_GROUP_NAME> . Hit Enter Key.
  4. Note that The TOP Include is create by default if the user checks the option of creating a TOP include.
How to create a Function Module
  1. Create a function Group (say "ZCAL").
  2. Create a function module, set the attributes like  (Function group, Application, Short Text and Process Type) and Save.
  3. Include file "LZCALU01" will have source code of first function module.
  4. Include file "LZCALTOP" will have global data.
  5. Main program "SAPLZCAL" contains
    • Global data Include file "LZCALTOP"
    • Function modules include file "LZCALUXX"
    • User defined Include files "LZCALF..", "LZCALO.." and "LZCALI.."
  6. Define interface parameters and Exceptions
  7. Write the source code
  8. Activate Function Module
  9. Testing the Function Module - Single Test & Debugging
  10. Documenting and Releasing a Function Module
That's all to Modularity in ABAP.

RFC Function Module


Following are the participant system for our demo purpose:
D05 – System where RFC function module exists
DS3 – Calling system (an RFC call is made from this system to D05)  
Scenario:
We would develop a small RFC function module in D05 system, which would add two variables. We would pass two variables from DS3 to D05 and get back the sum of those two variables back to DS3.  
Activities to be performed in the system D05
Go to SE37.
Under Utilities > Create Function Group (say ZGB_FUNC), if doesn’t exists.
Now create a New Function Module ZGB_FUNC01  
 
Under IMPORT Tab, define the following variables
Under EXPORT Tab  
 
In the Source Code
Now set the function module as “Remote-enabled module” in the attributes tab.  
 
Activities to be performed in the System DS3
Create an RFC destination from DS3 to D05.
Go to SM59, Press Create.
Enter the RFC Destination name: D05-205-TEST
Connection Type: 3
In Description: Test Connection to D05  
In The Technical Settings Tab, enter the system details
In The Logon/Security Tab, enter the logon details.
Click on Save. A Popup will imply that connection has been created  
Now Press the TEST CONNECTION to check the connection.  
This means the connection to the remote system was successful.
If the connection fails we get an error like this

However establishing a connection will not ensure logging on to the target system. For that we have to test REMOTE LOGON.
Press the REMOTE LOGON button
If we can see this
And this
Then our connection was successful.
However if we see the screen as shown below then there is an authentication failure (user-id or password is wrong).
The new connection will become visible in the SM59 list like this.
After establishing the connection, let us create a program in DS3 (calling system), which would trigger the RFC in D05  system.
Go to SE38 and create a program ZGB_RFC
Write the following code
Activate (Ctrl+F3) and Execute (F8)
Type any two numbers.
Press (F8) Again. We have the desired output.

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...