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: 

dynamic programming: send data to function module, mapping...

Former Member
0 Kudos

Hello abappers,

I have a requirement to be able to send a the name of a table and a table-field to a function module, it can be a table of any type, so the function module should be generic in nature, the function module receives the table name and table-field name and then selects data for the field specified from the table specified (in the import parameter of the FM). Within the function module, I want to be able to determine the fields type? and then send this field to an internal table representing a a destination table (also specified in the import parameters).

the idea is to be able to map a field from any table to a specified field in another table, the table(source and destination) and field names will only determined at run time as specified by the user.

any assistance will be appreciated, I am not very familiar with dynamic\generic programming.

3 REPLIES 3

former_member218674
Contributor
0 Kudos

Hello Bijo,

Following are the steps to implement this requirement:

1. Create import parameter for table name i.e. TABLE_NAME

2. Create tables parameter for fields table

3. Define table. DATA TABLE TYPE REF TO DATA. Also create single column internal table for fields and load all fields to select from database.

Inside function module you can implement following steps:

4. Load all the fields name into internal table. for this you can use DD03L for field details and use TABLE_NAME as key.

5. Once you have all the fields ready in internal table then construct fieldcatlog using these fields

loop at internal table with all the fields.

move it to field catalog.

append field catalog.

endloop.

6. Pass this field catalog table to static method create_dynamic_table

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = fieldcatalog_tab

importing

ep_table = table.

Here table is defined with data object type.

now assign table reference to field symbol of type table.

ASSIGN table->* to <field-tab>.

Also create work area <field-wa> using refrence of table.

create data object wa LIKE LINE OF <field-tab>.

ASSIGN wa->* to <field-wa>.

Also define field symbol for field name.

for e.g. <field_name>

7. Dynamic internal table is ready

Now using can use select as follows:

SELECT (fields) "--> fields is single column internal table to hold fields need to be selected

INTO CORRESPONDING FIELDS OF TABLE <itab> "--> itab created dynamically at runtime

FROM (TABLE_NAME) --> Table name imported

WHERE (where_clause). --> where_clause is single column internal table with data type CHAR72.

Select data into <itab> and use ASSIGN COMPONENT to move data from one table to another as follows:

8. To fill this dynamic internal table using ASSIGN COMPONENT <Comp_number> OF STRUCTURE <field-wa> TO <field-name>

So in this case if first field of structure STRUCT1 is user_id then sudo-code will be

loop at internal table containing list of fields into field_wa

ASSIGN COMPONENT field_wa OF STRUCTURE <field-wa> TO <field>. "Here field_wa is wa area for single column internal table holding all the fieldnames.

Now <field-name> points to user_id field. Move some value into it as nornally we do with variables.

move '001' to <field-name>.

or

<field-name> = '001'.

endloop.

after completing all the fields one row will be ready in <field_wa>.

APPEND <field_wa> to <field_tab>.

Let me know incase you face any issues!

Hope this helps you.

Thanks,

Augustin.

Edited by: Augustarian on Aug 23, 2009 7:02 PM

Edited by: Augustarian on Aug 23, 2009 7:08 PM

Edited by: Augustarian on Aug 23, 2009 7:09 PM

Former Member
0 Kudos

Hi,

I dont think there's anything dynamic in this.

Specify a table name type any or as LIST_COL_C-TABNAME in import parameters of the FM and a field name.

Try using FM K_LIST_GET_FIELDS_OF_STRUCTURE which gives you all fields of a table and call it internally in your FM.

In your export parameters specify a table of type LIST_COL_C-TABNAME which you can callback from your program.

Regards,

Amit

Former Member
0 Kudos

Thanks Augastrarian and Amit, I will try out your suggestions and report back.