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

How to call "dynamic" function?

Former Member
0 Likes
1,686

Hi,

I want to know if there is a way call a "dynamic" function.

I explain:

I may have many object (LFA1, LFB1, LFM1, KNA1, KNB1) and I want to call the function module *_TEXT_HEADER_SELECT.

Something like this.

CASE wvg_object.
 WHEN 'LFM1'.
        wvg_function = 'LFM1_TEXT_HEADER_SELECT'.
 WHEN 'LFA1'.
        wvg_function = 'LFA1_TEXT_HEADER_SELECT'.
WHEN OTHERS.
ENDCASE.

CALL FUNCTION wvg_function.

Possible to do this?

May you have some example to explain?

Thanks for your help.

David

1 ACCEPTED SOLUTION
Read only

Kanagaraja_L
Active Contributor
0 Likes
1,560

You can't directly call the Function Module, You have to generate the particular Function Module by

Function Module FUNCTION_STUB_GENERATE - It will generete your dynamic Function Module with corresponding Import and Export Parameter.

DATA funcname TYPE rs38l-name.
DATA source TYPE rswsourcet.
funcname = wvg_function.
CALL FUNCTION 'FUNCTION_STUB_GENERATE'
  EXPORTING
    funcname           = funcname
    ic_mode            = 'X'
    with_enhancements  = 'X'
  TABLES
    SOURCE             = SOURCE "-> You will get generated Function Module Here
  exceptions
    function_not_exist = 1
    OTHERS             = 2.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Hi,

I want to know if there is a way call a "dynamic" function.

I explain:

I may have many object (LFA1, LFB1, LFM1, KNA1, KNB1) and I want to call the function module *_TEXT_HEADER_SELECT.

Something like this.

CASE wvg_object.
 WHEN 'LFM1'.
        wvg_function = 'LFM1_TEXT_HEADER_SELECT'.
 WHEN 'LFA1'.
        wvg_function = 'LFA1_TEXT_HEADER_SELECT'.
WHEN OTHERS.
ENDCASE.

CALL FUNCTION wvg_function.

Possible to do this?

May you have some example to explain?

Thanks for your help.

David

8 REPLIES 8
Read only

andreas_mann3
Active Contributor
0 Likes
1,560

try sth like this:

concatenate wvg_object '*_TEXT_HEADER_SELECT' into wvg_function.

call function wvg_function...

A.

Read only

Former Member
0 Likes
1,560

Hi David,

DATA: wvg_function    type rs38l_fnam.     " the type is very important

CONCATENATE wvg_object 'TEXT_HEADER_SELECT' INTO wvg_function SEPARATED BY '_'.

CALL FUNCTION wvg_function
EXPORTING
...
IMPORTING
...
CHANGING
...

Remember all the functions you call dynamically must have the same parameters and when calling dynamically remember to put parameters correctly.

Hope this help you!

Regards,

Khanh

Read only

Kanagaraja_L
Active Contributor
0 Likes
1,561

You can't directly call the Function Module, You have to generate the particular Function Module by

Function Module FUNCTION_STUB_GENERATE - It will generete your dynamic Function Module with corresponding Import and Export Parameter.

DATA funcname TYPE rs38l-name.
DATA source TYPE rswsourcet.
funcname = wvg_function.
CALL FUNCTION 'FUNCTION_STUB_GENERATE'
  EXPORTING
    funcname           = funcname
    ic_mode            = 'X'
    with_enhancements  = 'X'
  TABLES
    SOURCE             = SOURCE "-> You will get generated Function Module Here
  exceptions
    function_not_exist = 1
    OTHERS             = 2.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Read only

0 Likes
1,560

Hi and thanks for your messages.

the difficulty is that the paramaters are different for each function module.

Only the name seems to be similar.

David

Edited by: David31 on Mar 31, 2010 2:25 PM

Edited by: David31 on Mar 31, 2010 2:30 PM

Read only

0 Likes
1,560

You can Get Function Module Import and Export Parameter Details From

Function Module "FUNCTION_IMPORT_INTERFACE"

Kanagaraja L

Read only

0 Likes
1,560

How do you execute the code generate by FUNCTION_STUB_GENERATE?

I have the code in an internal table.

Must I only loop on this?

Thanks!

David

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,560

Hello David,

SAP has provided a standard way of handling this situation. Read this online documentation: [http://help.sap.com/abapdocu_70/en/ABAPCALL_FUNCTION_DYNAMIC.htm|http://help.sap.com/abapdocu_70/en/ABAPCALL_FUNCTION_DYNAMIC.htm]

You can get the interface parameters using this piece of code:

TYPE-POOLS abap.

DATA: it_excep TYPE STANDARD TABLE OF rsexc,
      it_expar TYPE STANDARD TABLE OF rsexp,
      it_impar TYPE STANDARD TABLE OF rsimp,
      it_chpar TYPE STANDARD TABLE OF rscha,
      it_table TYPE STANDARD TABLE OF rstbl,
      wa_excep TYPE rsexc,
      wa_expar TYPE rsexp,
      wa_impar TYPE rsimp,
      wa_chpar TYPE rscha,
      wa_table TYPE rstbl,
      ptab      TYPE abap_func_parmbind_tab,
      ptab_line TYPE abap_func_parmbind.

PARAMETERS p_fname LIKE tfdir-funcname.

CALL FUNCTION 'FUNCTION_IMPORT_INTERFACE'
  EXPORTING
    funcname           = p_fname
  TABLES
    exception_list     = it_excep
    export_parameter   = it_expar
    import_parameter   = it_impar
    changing_parameter = it_chpar
    tables_parameter   = it_table
  EXCEPTIONS
    error_message      = 1
    function_not_found = 2
    invalid_name       = 3
    OTHERS             = 4.
IF sy-subrc = 0.
* Put your code for the dynamic function call here
ENDIF.

Hope this helps.

BR,

Suhas

Read only

0 Likes
1,560

Hi!

Sorry beeing so late.

Your answers are very helpfull!

Thanks,

David