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

Dynamic call of function modules

Former Member
0 Likes
14,963

I have an idea about using a DB table to define the calling of a function module. Now I know that you can call a FM named in a variable, but what about the Import/Export parameters?

Let's say that FM1 has the following parameters.

Import

i_par1

i_par2

export

e_par1

FM2 has

Import

i_par1

Export

e_par1

e_par2

e_par3

Is there a way to dynamicly call these 2 FMs with the same code? I realize that the parameters would have to be defined properly (data type) for it to work, but what about the number of import and export parameters? Or do I have to make a rule that any FM module that is defined in this DB table has to have the same parameter interface?

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
7,040

Yes it is possible pass the parameters in dynamic way. Check the online help on the CALL FUNCTION statement.

Check this example:


* copied from the online help on the CALL FUNCTION

TYPE-POOLS abap. 

DATA: line(80) TYPE c, 
      text_tab LIKE STANDARD TABLE OF line, 
      filename TYPE string, 
      filetype(10) TYPE c, 
      fleng TYPE i. 

DATA: func TYPE string, 
      ptab TYPE abap_func_parmbind_tab, 
      ptab_line TYPE abap_func_parmbind, 
      etab TYPE abap_func_excpbind_tab, 
      etab_line TYPE abap_func_excpbind. 

func = 'GUI_DOWNLOAD'. 
filename = 'c:\temp\text.txt'. 
filetype = 'ASC'. 

ptab_line-name = 'FILENAME'. 
ptab_line-kind = abap_func_exporting. 
GET REFERENCE OF filename INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

ptab_line-name = 'FILETYPE'. 
ptab_line-kind = abap_func_exporting. 
GET REFERENCE OF filetype INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

ptab_line-name = 'DATA_TAB'. 
ptab_line-kind = abap_func_tables. 
GET REFERENCE OF text_tab INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

ptab_line-name = 'FILELENGTH'. 
ptab_line-kind = abap_func_importing. 
GET REFERENCE OF fleng INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

... 

etab_line-name = 'OTHERS'. 
etab_line-value = 10. 
INSERT etab_line INTO TABLE etab. 

CALL FUNCTION func 
  PARAMETER-TABLE 
    ptab 
  EXCEPTION-TABLE 
    etab. 

CASE sy-subrc. 
  WHEN 1. 
    ... 
  ... 
ENDCASE.  

Regards,

Naimesh Patel

11 REPLIES 11
Read only

former_member194669
Active Contributor
0 Likes
7,040

Define your import parameters as OPTIONAL( You can find checkbox in the import tab of se37) so if you are passing or NOT passing will not be a issue

a®

Read only

0 Likes
7,040

But how do you write the code that actually calls the FM? Especially how do I code the parameter list?

Sometime the FM will have 3 import parameters while other times it might have 15 or 20. How do I code that? Is there a way to say, "here is an internal table containing my Import parameters and another that contains my export parameters"?

I was hoping for maybe something like:

CALL FUNCTION fmvariablename

EXPORTING PARAMETER TABLE t_exptable

IMPORTING PARAMETER TABLE t_imptable.

where t_exptable would have a structure something like this:

exptable

....parametername

....parametervalue variable or <field symbol> name

Or maybe I could build the function call simular to the way a dynamic SQL statement is built.

I am thinking that this is not really possible, but I thought I would ask. If you know that this is not possible please tell me that too.

I had another thought. If this cannot be done with a FM, how about a method call? I don't know too much about OO ABAP, but am wanting to learn.

Edited by: Larry Browning on Mar 18, 2009 10:37 AM

Read only

0 Likes
7,040

I think your issue with export table that comes out of function module. For handling this you need to use the logic of creating dynamic table or fill it in a flat structure ( please check fm RFC_READ_TABLE )

a®

Read only

0 Likes
7,040

Hmm, I looked at that FM and am not sure how that would help. Apparently my brain hasn't formed a connection with this and my question. Can you expand on this please?

Read only

0 Likes
7,040

Hi Larry

I don't think OOPS might come to rescue here.

I remember there is a table which holds the export import parametrers of the fms (at least there oughtr to be).

but the thing is there aer only sso many things to take care here, like matching the data type.

maybe you could do some thing with the CASE statemwent.

but that'd entail a pretty good number of combinations.

seems bleak to me.

Pushpraj

Read only

0 Likes
7,040

Say for example in your exporting table in SE37 table tab


DATA	LIKE	YTAB1500	Data read (out)   " Flat structure with a length of 1500 characters

then


  data: dref type ref to data.           
  field-symbols: <wa> type any, <comp> type any.
  create data dref type (query_table).   " Import parameter for example table name to query
  assign dref->* to <wa>.                "
    select * from (query_table) into <wa> where (options).
     write : <wa> to wa_data.
     append wa_dta to data.
    endselect.
  endif.

a®s

May this will help you out

Read only

0 Likes
7,040

a®s,

I am aware of dynamic internal tables and in fact I am using them in a different area of the very same program I am wanting to do a dynamic call to FM with. However, I have not yet seen the connection to how this could be used to answer my question. As best as I can tell I would need to be able to somehow specify dynamically the parameter lists (parameter name and the value passed of that parameter) . I don't know how to do this or even if it can be done at all.

I really do appreciate your help with my question, but I am just not getting the connection to what you are suggesting and my question.

Read only

Former Member
0 Likes
7,040

Hi Larry

Can't you use an IF ELSE ENDIF if it's just about two FMs?

Pushpraj

Read only

0 Likes
7,040

No, I was hoping for something where I could call just about any FM specified in the DB table. It would have to be designed so that the parameters would also be specified in the table as well.

Read only

naimesh_patel
Active Contributor
7,041

Yes it is possible pass the parameters in dynamic way. Check the online help on the CALL FUNCTION statement.

Check this example:


* copied from the online help on the CALL FUNCTION

TYPE-POOLS abap. 

DATA: line(80) TYPE c, 
      text_tab LIKE STANDARD TABLE OF line, 
      filename TYPE string, 
      filetype(10) TYPE c, 
      fleng TYPE i. 

DATA: func TYPE string, 
      ptab TYPE abap_func_parmbind_tab, 
      ptab_line TYPE abap_func_parmbind, 
      etab TYPE abap_func_excpbind_tab, 
      etab_line TYPE abap_func_excpbind. 

func = 'GUI_DOWNLOAD'. 
filename = 'c:\temp\text.txt'. 
filetype = 'ASC'. 

ptab_line-name = 'FILENAME'. 
ptab_line-kind = abap_func_exporting. 
GET REFERENCE OF filename INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

ptab_line-name = 'FILETYPE'. 
ptab_line-kind = abap_func_exporting. 
GET REFERENCE OF filetype INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

ptab_line-name = 'DATA_TAB'. 
ptab_line-kind = abap_func_tables. 
GET REFERENCE OF text_tab INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

ptab_line-name = 'FILELENGTH'. 
ptab_line-kind = abap_func_importing. 
GET REFERENCE OF fleng INTO ptab_line-value. 
INSERT ptab_line INTO TABLE ptab. 

... 

etab_line-name = 'OTHERS'. 
etab_line-value = 10. 
INSERT etab_line INTO TABLE etab. 

CALL FUNCTION func 
  PARAMETER-TABLE 
    ptab 
  EXCEPTION-TABLE 
    etab. 

CASE sy-subrc. 
  WHEN 1. 
    ... 
  ... 
ENDCASE.  

Regards,

Naimesh Patel

Read only

0 Likes
7,040

Naimesh,

That looks like exactly what I am wanting to do! I can't believe I didn't simply look at the help on CALL FUNCTION. I did look at my ABAP quick reference card that is pretty old (version 4.5) and it did not have the PARAMETER-TABLE addition in it. We are now at 6.20 and upgrading to 7.0 this summer, so maybe I should throw that card away!

Sorry, My bad. I will award you points!!!!