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 Modules as Methods

prasad_reddy9
Explorer
0 Likes
1,188

I want to call few FM as methods. ANyone has ANy sample example for it?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,126

Hi Prasad,

Syntactical constraints will not allow you to call function modules as methods. What you can do is define some methods in a class and inside the method implementation just call the FM in the same way as you call it from some other place. Then call the method using the syntax used for calling methods.

CLASS zl_lcl_vehicle DEFINITION.

PUBLIC SECTION.

METHODS: set_make

IMPORTING im_make TYPE string

im_model TYPE string.

ENDCLASS. "zl_lcl_vehicle DEFINITION

CLASS zl_lcl_vehicle IMPLEMENTATION.

METHOD set_make.

call function <FM name>.

ENDMETHOD. "set_make

ENDCLASS. "zl_lcl_vehicle

START-OF-SELECTION.

  • Create instance of the vehicle class

CREATE OBJECT z_vehicle1.

  • Normal Syntax for method calls

CALL METHOD: z_vehicle1->set_make EXPORTING im_make = 'HYUNDAI'

im_model = 'SANTRO'.

<b>Award points if found useful.</b>

Regards

Indrajit

9 REPLIES 9
Read only

Former Member
0 Likes
1,127

Hi Prasad,

Syntactical constraints will not allow you to call function modules as methods. What you can do is define some methods in a class and inside the method implementation just call the FM in the same way as you call it from some other place. Then call the method using the syntax used for calling methods.

CLASS zl_lcl_vehicle DEFINITION.

PUBLIC SECTION.

METHODS: set_make

IMPORTING im_make TYPE string

im_model TYPE string.

ENDCLASS. "zl_lcl_vehicle DEFINITION

CLASS zl_lcl_vehicle IMPLEMENTATION.

METHOD set_make.

call function <FM name>.

ENDMETHOD. "set_make

ENDCLASS. "zl_lcl_vehicle

START-OF-SELECTION.

  • Create instance of the vehicle class

CREATE OBJECT z_vehicle1.

  • Normal Syntax for method calls

CALL METHOD: z_vehicle1->set_make EXPORTING im_make = 'HYUNDAI'

im_model = 'SANTRO'.

<b>Award points if found useful.</b>

Regards

Indrajit

Read only

prasad_reddy9
Explorer
0 Likes
1,126

Thx for the reply.

Just few clarifications...

So, the importing, exporting and changing parameters of the FM must match the Methods.

What abt 'table' option option?

DO we handle handle exceptions as events or just let the called FM raise its own messages?

Read only

0 Likes
1,126

Hi Prasad,

What you can do is -

Import a parameter in method - Export it to FM if required

Import a parameter from FM - Use it in the method if required.

The table option is for importing/exporting tables.

The function module will handle the exceptions defined in its interface and return the value in sy-subrc. You can check it and code some functionality depending on the value of sy-subrc. This depends on your requirement. If the FM does not handle any exception that you want to handle, you can handle it explicitly using exception classes and TRY-CATCH statements.

Let me know if you need any further clarifications.

Award points if found useful.

Regards

Indrajit

Read only

prasad_reddy9
Explorer
0 Likes
1,126

It is helpful.

Can u elaborate on the tables option?

I know how to use it in FM's but in classes how do u handle it, especially for passing it to the FM inside the method? Just declare it in changing type of the class?

Read only

0 Likes
1,126

Hi Prasad,

See the following piece of code.

TYPES: ty_itab TYPE standard table OF sflight.

&----


*& Class (Definiton) zl_lcl_vehicle

&----


  • Definition of Vehicle class

----


CLASS zl_lcl_vehicle DEFINITION.

PUBLIC SECTION.

METHODS: set_make

IMPORTING value(im_itab) TYPE ty_itab.

PRIVATE SECTION.

DATA: gt_itab TYPE STANDARD TABLE OF sflight.

ENDCLASS. "zl_lcl_vehicle DEFINITION

&----


*& Class (Implementation) zl_lcl_vehicle

&----


  • Implementation of Vehicle class

----


CLASS zl_lcl_vehicle IMPLEMENTATION.

METHOD set_make.

CALL FUNCTION 'Z_MOBILE_NUMBER_DIST'

TABLES

t_in_slifght = im_itab.

ENDMETHOD. "set_make

ENDCLASS. "zl_lcl_vehicle

  • Declaring a reference variable with reference to the vehicle class.

  • This vareiable contains a reference to an object of theclass

DATA z_vehicle1 TYPE REF TO zl_lcl_vehicle.

DATA : gt_itab TYPE STANDARD TABLE OF sflight.

START-OF-SELECTION.

  • Create instance of the vehicle class

CREATE OBJECT z_vehicle1.

  • Normal Syntax for method calls

CALL METHOD: z_vehicle1->set_make EXPORTING im_itab = gt_itab.

_________________________________________________________________

<b>Award points if found useful.</b>

Regards

Indrajit

Read only

0 Likes
1,126

> It is helpful.

>

> Can u elaborate on the tables option?

>

> I know how to use it in FM's but in classes how do u

> handle it, especially for passing it to the FM inside

> the method? Just declare it in changing type of the

> class?

short answer: yes

TABLES parameters are forbidden in OO context. You should not use them. They work only with STANDARD TABLE WITH HEADER LINE, which are forbidden in ABAP OO. Even when working with FMs you should not use them.

Read only

prasad_reddy9
Explorer
0 Likes
1,126

Just tried it.

Seems FM may need to be called 'In Background Task' !!

And I cant seem to make the FM accpept importing parameters from the method.

Read only

0 Likes
1,126

Hi Prasad,

In the FM define a TABLES parameter and while calling the FM assign the table imported by the method to it.

Award points if useful.

Regards

Indrajit

Read only

prasad_reddy9
Explorer
0 Likes
1,126

We can always declare tables without header line. Thats not a problem I think.

I tried to do this and the FM inside the method was not accepting the input parameters from the method.

If Anyone has a sample code for this type of a thing, it would be of immense help.