Application Development 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: 

Function Module

Former Member
0 Kudos
97

Hi all,

I have to create a function module to fetch the content from table by entering a value .

Can anyone tell me the procedure for this.

I created but i am not getting the records.

thanx,

krishna.

6 REPLIES 6

Former Member
0 Kudos
54

Hi,

If you post your code, the problem can be found easily. However, here is the procedure.

Say, you want to get material details like material type based on matnr.

create an import parameter -v_matnr

create an export paramter - v_mtart(material type)

write select statement

select mtart from mara into v_mtart where matnr = v_matnr.

save and activate.

if you want more than one field define a structure and create a table in table parameter say itab.

Select the data into this table

select  mtart ....
  from mara
  into table itab
 where matnr = v_matnr.

Regards,

Richa

Former Member
0 Kudos
54

HI,

in the import parameters

give the field name : so that it can accept the value

now in the tables tab give the output table name which is of

type database table

now in the source code

select * from dbtable into table (itab) where

field = import parameter.

if sy-subrc ne 0.

error message ( declare the error message type in export parameter

or else you can also declare as table depending on your requirement )

exit.

endif.

reward if helpful

regards,

venkatesh

Former Member
0 Kudos
54

Hi,

Creating Function Modules

You can only create function modules and function groups using the Function Builder in the ABAP Workbench. For further information, refer to Creating New Function Modules. 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.

Function Groups and Function Modules

Firstly, we create a new function group DEMO_SPFLI to hold the function module (see Creating a Function Group). Then, we can create the new function module (see Creating a Function Module).

Parameter Interface

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.

The following type specification would have the same effect:

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.

We can now use this data type to specify the type of the export parameter ITAB:

The internal table is passed by value. Only internal tables that are passed using tables parameters can be passed exclusively by reference.

Exceptions

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:

Source Code

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 of the exception or OTHERS occurs in the EXCEPTIONS addition of the CALL FUNCTION statement, the exception is handled by the calling program.

If the calling program does not handle the exception

The RAISE statement terminates the program and switches to debugging mode.

The MESSAGE ..... RAISING statement display the specified message. How the processing continues depends on the message type.

If the calling program handles the exception, both statements return control to the program. No values are transferred. The MESSAGE ..... RAISING statement does not display a message. Instead, it fills the system fields SY-MSGID, SY-MSGTY, SY-MSGNO, and SY-MSGV1 to SY-MSGV4.

Source Code of READ_SPFLI_INTO_TABLE

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.

Calling READ_SPFLI_INTO_TABLE

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.


Reward points

regards

Former Member
0 Kudos
54

Hi Krishna ,

Did you debug the program and see what is the reason for it.

One reason which i feel is that you are not passing the correct data , what i mean by this is that you need to call the conversion exit before you pass the data to the select statement in the FM , what generally happens is that there are few leading 0's which we miss out in select statement , so we do not get any output.

Please check this and feel free to revert back in case you still have issues

Regards

Arun

Former Member
0 Kudos
54

Hi,

Are you getting any error or the select query not returning anything.

****

The select query results would be saved in the internal table.

This internal table is to be declared in the "tables" parameters.

And then could be used at the place where you are calling this FM.

Please reward if useful.

regards,

Former Member