Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Function Module

Former Member
0 Likes
478

Hi all ,

Can any one help me in creating my own function module ? A simple example will be of very good help .

Plz tell me in brief about all the parametrs or tabs in se37 .

4 REPLIES 4
Read only

Former Member
0 Likes
438

Hi,

T-code for Function Module : SE37

You can only create function modules and function groups using the Function Builder in the ABAP Workbench.

An example to illustrate how a function module is created from the point of view of

ABAP programming:

1. Firstly, we create a new function group DEMO_SPFLI to hold the function module.Then, we can create the new function module.

2. You can specify the types of interface parameters in function modules in the

same way as the parameter interfaces of subroutines. Since function

modules can be used anywhere in the system, their interfaces can only contain

references to data types that are declared systemwide.

The function module READ_SPFLI_INTO_TABLE requires an import parameter

to restrict the selection to a single airline. To specify the type, we can refer to the

key field CARRID of the database SPFLI.

3. To pass data back to the calling program, the function module needs an export

parameter with the type of an internal table. For this, we define a systemwide

table type SPFLI_TAB with the line type SPFLI in the ABAP Dictionary.

4. Our function module needs an exception that it can trigger if there are no entries

in table SPFLI that meet the selection criterion. The exception NOT_FOUND

serves this function.

5. Having defined the parameter interface and exceptions, we can now write the

source code of our function module. To do this, choose Source code in the

Function Builder. This opens the ABAP Editor for the include program

L<fgrp>U<xx>. This is the include that will hold the program code for the function module.

The following program calls the function module READ_SPFLI_INTO_TABLE:

REPORT DEMO_FUNCTION_MODULE.

PARAMETERS CARRIER TYPE S_CARR_ID.

DATA: JTAB TYPE SPFLI_TAB,

WA LIKE LINE OF JTAB.

CALL FUNCTION 'READ_SPFLI_INTO_TABLE'

EXPORTING

ID = CARRIER

IMPORTING

ITAB = JTAB

EXCEPTIONS

NOT_FOUND = 1

OTHERS = 2.

CASE SY-SUBRC.

WHEN 1.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO.

WHEN 2.

MESSAGE E702(AT).

ENDCASE.

LOOP AT JTAB INTO WA.

WRITE: / WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO.

ENDLOOP.

The actual parameters CARRIER and JTAB have the same data types as their

corresponding interface parameters in the function module. The exception

NOT_FOUND is handled in the program. It displays the same message that the

function module would have displayed had it handled the error.

Read only

Former Member
0 Likes
438

Hi,

<u><b>Creating Function Modules</b></u>

You can only create function modules and function groups using the Function Builder in the ABAP Workbench. This section uses an example to illustrate how a function module is created from the point of view of ABAP programming.

We are going to create a function module READ_SPFLI_INTO_TABLE to read

data for a specified airline from table SPFLI into an internal table, which it then

passes back to the calling program.

<b>Function Groups and Function Modules</b>

Firstly, we create a new function group DEMO_SPFLI to hold the function module. Then, we can create the new function module.

<b>Parameter Interface</b>

You can specify the types of interface parameters in function modules in the

same way as the parameter interfaces of subroutines. Since function

modules can be used anywhere in the system, their interfaces can only contain

references to data types that are declared systemwide. These are the elementary

ABAP data types, the systemwide generic types, such as ANY TABLE, and types

defined in the ABAP Dictionary. You cannot use LIKE to refer to data types declared in the main program.

The function module READ_SPFLI_INTO_TABLE requires an import parameter

to restrict the selection to a single airline. To specify the type, we can refer to the

key field CARRID of the database SPFLI

Under Ref. field/structure, you can enter a column of a database table, a component of a ABAP Dictionary structure, or a whole ABAP Dictionary structure.

The import parameter ID is optional, and has a default value.

Ref. type can contain any generic or full data type that is recognized systemwide.

Here, the parameter is defined with reference to the elementary ABAP Dictionary

type (or data element) S_CARR_ID. This is the type used to define the field

SPFLI-CARRID.

To pass data back to the calling program, the function module needs an export

parameter with the type of an internal table. For this, we define a systemwide

table type SPFLI_TAB with the line type SPFLI in the ABAP Dictionary.

The internal table is passed by value. Only internal tables that are passed using

tables parameters can be passed exclusively by reference.

<b>Exceptions</b>

Our function module needs an exception that it can trigger if there are no entries

in table SPFLI that meet the selection criterion. The exception NOT_FOUND

serves this function.

<b>Source Code</b>

Having defined the parameter interface and exceptions, we can now write the

source code of our function module. To do this, choose Source code in the

Function Builder. This opens the ABAP Editor for the include program L<fgrp>U<xx>. This is the include that will hold the program code for the function module;

The source code of the function module occurs between the FUNCTION and

ENDFUNCTION statements. The definitions of the parameter interface and the

exceptions is displayed here in comment lines. Its real coding is generated

invisibly by the Function Builder.

The entire source code of READ_SPFLI_INTO_TABLE looks like this:

FUNCTION READ_SPFLI_INTO_TABLE.

*"----


--

""Local interface:

*" IMPORTING

*" VALUE(ID) LIKE SPFLI-CARRID DEFAULT 'LH '

*" EXPORTING

*" VALUE(ITAB) TYPE SPFLI_TAB

*" EXCEPTIONS

*" NOT_FOUND

*"----


--

SELECT * FROM SPFLI INTO TABLE ITAB WHERE CARRID = ID.

IF SY-SUBRC NE 0.

MESSAGE E007(AT) RAISING NOT_FOUND.

ENDIF.

ENDFUNCTION.

The function module reads all of the data from the database table SPFLI where

the key field CARRID is equal to the import parameter ID and places the entries

that it finds into the internal table SPFLI_TAB. If it cannot find any entries, the

exception NOT_FOUND is triggered using MESSAGE...RAISING. Otherwise, the

table is passed to the caller as an exporting parameter.

<b>Calling READ_SPFLI_INTO_TABLE</b>

The following program calls the function module READ_SPFLI_INTO_TABLE:

REPORT DEMO_FUNCTION_MODULE.

PARAMETERS CARRIER TYPE S_CARR_ID.

DATA: JTAB TYPE SPFLI_TAB,

WA LIKE LINE OF JTAB.

CALL FUNCTION 'READ_SPFLI_INTO_TABLE'

EXPORTING

ID = CARRIER

IMPORTING

ITAB = JTAB

EXCEPTIONS

NOT_FOUND = 1

OTHERS = 2.

CASE SY-SUBRC.

WHEN 1.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO.

WHEN 2.

MESSAGE E702(AT).

ENDCASE.

LOOP AT JTAB INTO WA.

WRITE: / WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO.

ENDLOOP.

The actual parameters CARRIER and JTAB have the same data types as their

corresponding interface parameters in the function module. The exception

NOT_FOUND is handled in the program. It displays the same message that the

function module would have displayed had it handled the error.

Regards,

Bhaskar

Read only

Former Member
0 Likes
438

Hi,

<a href="http://www.saptechnical.com/Tutorials/ABAP/RFCCall/Page1.htm">FM</a>

reard if useful.