Step-by-Step Guide: Creating a Function Module in SAP ABAP

How To Create Function Module In Sap Abap

Procedure In your ABAP project, select the relevant Package node in the Project Explorer. Expand the Source Library folder and select the relevant Function Group. Open the context menu and select New ABAP Function Module . In the following dialog, enter a unique name for the new function group in the Name field. More items

What Does Function Module Consist Of?

Import Parameters: These are the inputs that are passed from the main program to the function module.

Export parameters in SAP ABAP function modules refer to the outputs of the function module. These parameters are initially sent from the main program without any values and are returned back to the main program with their respective output values.

Modifying Parameters: These are input parameters that, after the execution of the function module, undergo changes and subsequently serve as output parameters.

Exception Parameters are parameters that trigger specific exceptions that our code anticipates.

Creating FM in SE37: A Step-by-Step Guide for India

To execute SE37, follow these steps:

1. Enter the function module name as ZAMARMN_DIVIDE and select Create.

2. Enter the function group as ZAMARMN_FG and provide a short text.

3. Click Save to save your changes.

4. If prompted with a dialog stating that the function module name is reserved for SAP, click Continue.

Additional items may be required depending on the specific context or requirements of the task at hand.

Step-by-Step Guide on Creating Function Module in SAP ABAP

We will be developing a function module that performs calculations on two operands.

The values IV_OPERAND_1 and IV_OPERAND_2 are required.

The outcome of the calculation is determined by the type of operation specified in the IV_OPERATION parameter. There are two potential operations that can be performed.

The two basic arithmetic operations in SAP ABAP are adding (+) and subtracting (-).

You might be interested:  Using Which Sap Client to Configure Stms

The outcome of the computation is saved in the EV_RESULT parameter.

In case the IV_OPERATION is neither addition (+) nor subtraction (-), the function module will generate an exception known as OPERATION_NOT_SUPPORTED.

How to create a function module in SAP using Tcode?

To create a function module in SAP ABAP, follow these steps. First, go to the SE80 transaction and select “Function group” from the drop-down list box. This will allow you to create a new function group for your module.

Once the function group is created, you can begin creating individual function modules within it. These modules will contain the actual logic and functionality of your program or application.

Creating a Function Module in SAP ABAP: Step-by-Step Guide

To access Function Builder in SAP, navigate to SE37 transaction from the SAP easy access menu.

Please be aware that when using the transaction code SE37, adding /OSE37 will open a new session or window.

Enter the new “Function Module” name. In my case I will name it: ZAS_LIB_HPL_CALCULATE_DEMO.

In SAP, it is crucial to follow a naming convention for Function Modules. Typically, the name of a Function Module begins with either Z or Y. In my case, I prefer using AS as initials in the names. Adhering to this convention enhances readability and makes it easier to maintain the code in SAP ABAP.

Enter the “Function group”. The function group must be defined before the creation of the function module.

Enter the “Short text” to describe the function module, e.g. “ABAP Academy Demo Calculate FM”

An informative pop-up appears with the given notification. To continue, click on the green button.

“Attributes” Tab

On this tab, we present all the input parameters for the function module.

Checkbox “Optional” means that the current parameter is optional and does not need to be filled.

There are three parameters that need to be imported for this calculate function module.

  • The first input parameter is called IV_OPERAND_1.
  • The second input parameter is known as IV_OPERATION.
  • The third input parameter is referred to as IV_OPERAND_2.

.

Importing parameter starts with “IV”, which stands for “Importing Variable”. This naming convention is important for readability and maintainability. By doing this, we know that all parameters starting with “IV” are importing parameters.

The values of IV_OPERAND_1 and IV_OPERAND_2 are integers, representing numerical content.

The IV_OPERATION variable is a one-character type that will hold either a plus (+) or minus (-) sign.

You might be interested:  Understanding the Role of Plant in SAP MM

“Export” Tab

On this tab, we offer all the exporting parameters for the function module. For this specific calculation function module, there is only one exporting parameter called EV_RESULT. Its data type is Integer since it stores the calculated result.

The name of the exporting parameter begins with EV, which represents Exporting Variable.

“Changing” Tab

On this tab, we present all the variables that can be modified for the function module. The calculation function module does not have any adjustable variables.

To illustrate the process of creating a changing parameter, I have created an example called CV_DEMO. The name of the changing parameter begins with “CV,” which is short for Changing Variable.

Please ensure that you remove this parameter as it will not be utilized in the given example.

“Exception” Tab

In this tab, we create an exception(s) to catch certain types of errors. As our calculation function module only will use addition (+) and subtraction (-), we need to create an error exception when the operation that is sent by the main program is not supported.

This is the real ABAP Editor where we execute the code for the Function Module.

ABAP offers predefined comments that include the different types of parameters: Import, Export, Changing parameters, and Exceptions.

Code implementation

This piece of code performs calculations on two variables, namely IV_OPERAND_1 and IV_OPERAND_2.

The value in IV_OPERATION can either be added (+) or subtracted (-).

Initially, I empty the exporting parameter EV_RESULT. This is done to visually indicate that this parameter is meant for exporting purposes and it should not contain any value at the beginning of the logic execution.

Next, I search for the operation in IV_OPERATION. If:

The value of IV_OPERATION is the plus sign (+), which means that we need to add IV_OPERAND_1 and IV_OPERAND_2 together.

Subtract the value of IV_OPERAND_2 from IV_OPERAND_1 using the subtraction operation represented by the IV_OPERATION value.

If the value of IV_OPERATION is neither addition (+) nor subtraction (-), an exception will be raised.

Verify the coherence (CTRL+F2) and enable the function (CTRL+F3).

The function module ZAS_LIB_HPL_CALCULATE_DEMO has been successfully created.

To check the functionality of the function module, simply press (F8) to execute it. After doing so, a screen will appear as shown below:

To carry out the calculation, input the values of IV_OPERAND_1, IV_OPERATION, and IV_OPERAND_2 and then click on Execute (F8).

You might be interested:  Tips for Advancing in a SAP Career

The value of EV_RESULT is populated with the outcome of the calculation, as illustrated in the following manner:

If the IV_OPERATION entered is multiplication (*), it raises an exception because the operation is not supported.

Creating a function module: How is it done?

To create a function module in SAP ABAP, follow these steps:

1. In your ABAP project, navigate to the relevant Package node in the Project Explorer.

3. Right-click on the Function Group and choose “New ABAP Function Module” from the context menu.

To create a function module in SAP ABAP, please follow these steps:

1. Open your ABAP project and locate the relevant Package node within the Project Explorer.

3. Right-click on it to open up a context menu, then choose “New ABAP Function Module”.

4. A dialog box will appear where you need to enter a distinctive name for your new function module into the designated Name field.

Is it possible to create a function module without a function group?

While creating a function module itself the system asks for an already created function group(or you have to create one while creating fm). The program might throw a syntax error or go to dump when any program tries to access the function module.

Using FM in SAP: A Guide

You can call a function module from any ABAP program by using the following ABAP statement:CALL FUNCTION &lt,function module name&gt,[IMPORTING f1 = a1

– . fn = an][CHANGING f1 = a1

– . fn = an][TABLES t1 = itab1

– . tn = itabn][EXCEPTIONS e1 = r1

– . en = rn][ERROR_MESSAGE = rE][OTHERS = ro]].More items

Creating a function module exit in SAP: How is it done?

Once you have accessed the development class, you can proceed with creating a function module. A function module is a reusable piece of code that performs specific tasks within an SAP system. It encapsulates a set of related functionalities that can be called from various programs or transactions.

After defining the basic details of your function module, you can start implementing its logic by writing ABAP code within it. The code should include all necessary instructions to perform the desired functionality when called from other programs or transactions.

Finally, save your changes and activate the newly created function module using Tcode SE80 or any other relevant activation method available in your SAP system. This step ensures that your custom logic is ready for execution whenever required.