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

Return Dynamic Structure in Function Module

Former Member
0 Likes
8,833

Hello,

I have poured through the discussions and have not found an answer to this particular question.

What I want to do is in a Function Module, is build a structure of a SAP table based on the table name pass to the function module. I do not want to pass back a table nor XML.  Just EXPORT the structure like if you were defining the parameter in the Function Module with an Associated Type.

Can someone help me?

Regards, Dean.

13 REPLIES 13
Read only

former_member210008
Active Participant
0 Likes
5,855

AFAIK it's not possible because FM parameters should be defined in dictionary. But you can export fieldcatalog to create your dynamic structure/table later.

Read only

matt
Active Contributor
0 Likes
5,855

Pass a ref to data.

Read only

Former Member
0 Likes
5,855

Hello Matthew,

Can you elaborate?

Read only

Former Member
0 Likes
5,855

A Ref to data is an object defined as 'Type Ref To Data'.  This is populated by a reference to whatever object you like using the command:


Get Reference Of my_object into o_Ref.

By then DeReferencing that reference into a Field Symbol you can access the orginal object as Normal:


Assign o_Ref->* to <f_Original>.

Since Type Ref To Data is just one type you can pass back muitple different 'actual' data types using the same parameter.

Rich

Read only

Former Member
0 Likes
5,855

Or,  as I did on a previous project,  use a table that contained a structure name and a table of XSEQUENCEs.

Then use CL_ABAP_CONV_OUT_CE to populate that in the FM,  and CL_ABAP_CONV_IN_CE to decode it in the calling program using the associated structure name.

In that way you can pass mutliple tables or structures using just one table parameter and without having to keep the original data hanging around.

Rich

Read only

former_member378318
Contributor
0 Likes
5,855

Not quite sure what you are trying to achieve but have a look at this:

FUNCTION ztest.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(TABNAME) TYPE  TABNAME DEFAULT 'T100'
*"  CHANGING
*"     REFERENCE(ET_TAB1) TYPE  ANY TABLE
*"----------------------------------------------------------------------

DATA: l_dref_table TYPE REF TO data.

FIELD-SYMBOLS: <fs_tabnam> TYPE STANDARD TABLE.

CREATE DATA l_dref_table TYPE STANDARD TABLE OF (tabname).
ASSIGN l_dref_table->* TO <fs_tabnam>.

APPEND INITIAL LINE TO <fs_tabnam>.
et_tab1[]
= <fs_tabnam>[].

ENDFUNCTION.




Read only

0 Likes
5,855

Hello Surbjeet,

I tried this...

FUNCTION ZTEST
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_TABNAME) TYPE TABNAME
*"  EXPORTING
*"     REFERENCE(ES_STRUCTURE)


*-- Create Internal Table Structure
   perform create_dynamically.

* Create dynamic work area and assign to Field Symbol
   create data gdyn_line like line of <gfs_table>.


*-- Capture Key Structure
   assign gdyn_line->* to <gfs_line>.
   ES_STRUCTURE = <gfs_line>.


ENDFUNCTION.


This didn't work. Is only by passing a table the only possibility?

Read only

0 Likes
5,855

Hi Dean,

Which bit is not working? Can you explain you requirement more clearly? If you only want to pass back a structure try this:

FUNCTION ztest.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(TABNAME) TYPE  TABNAME DEFAULT 'T100'
*"  CHANGING
*"     REFERENCE(E_TAB_STRUC) TYPE  ANY
*"----------------------------------------------------------------------

DATA: l_dref_table_workarea TYPE REF TO data.

FIELD-SYMBOLS <fs> TYPE any.

CREATE DATA l_dref_table_workarea TYPE (tabname).
ASSIGN l_dref_table_workarea->* TO <fs>.

e_tab_struc 
= <fs>.

ENDFUNCTION.

Read only

0 Likes
5,855

Hell Surbjeet,

When I do this and test the FM, I am prompted for a value for the E-TAB_STRUC and during execution I get a dump with this error...

Conversion of type "TABLE OF FLAT_STRUCTURE" to type "C LENGTH 200" not

supported.

Any suggestions?

Read only

0 Likes
5,855

In the second example that I posted the changing parameter E_TAB_STRUC is of TYPE ANY (in the first example it was TYPE ANY TABLE). Make sure you declare this properly.

The second example can be called like this:

DATA: lw_t100 TYPE t100.

CALL FUNCTION 'ZTEST'

   EXPORTING

     tabname     = 'T100'

   CHANGING

     e_tab_struc = lw_t100.

Read only

0 Likes
5,855

When I do that I get..

Error generating the test frame

Message No. FL819

Diagnosis

The system could not generate a syntactically correct test frame for function module ZTEST You therefore cannot test the function module using the test environment. You have probably used an ABAP feature in the interface definition that is not yet supported in the test environment.

The error message is:

You cannot use generic types for fields. The table types "ANY" and "INDEX" are generic

System Response

Procedure

Check whether you can change the interface of the function module so that it is possible to generate the test frame. If this is not possible,you will have to write your own utility for testing the function module.

Procedure for System Administration

Read only

0 Likes
5,855

Here is the code and the results..

FUNCTION ZTEST .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(TABNAME) TYPE  TABNAME
*"  CHANGING
*"     REFERENCE(EC_STRUCTURE) TYPE  ANY
*"----------------------------------------------------------------------

DATA: l_dref_table_workarea TYPE REF TO data.

FIELD-SYMBOLS <fs> TYPE any.

CREATE DATA l_dref_table_workarea TYPE (tabname).
ASSIGN l_dref_table_workarea->* TO <fs>.

EC_STRUCTURE  = <fs>.


ENDFUNCTION.


Execute...

Press SAVE and results are..

Read only

0 Likes
5,854

Hi Dean,

You need to fill the return structure however you want, I left it empty. Add these lines to see data returned in the structure:

DATA: l_dref_table_workarea TYPE REF TO data.

   FIELD-SYMBOLS <fi> TYPE any.

   FIELD-SYMBOLS <fs> TYPE any.

   CREATE DATA l_dref_table_workarea TYPE (tabname).

   ASSIGN l_dref_table_workarea->* TO <fs>.

   ASSIGN COMPONENT 'SPRSL' OF STRUCTURE <fs> TO <fi>.

   <fi> = sy-langu.

   ASSIGN COMPONENT 'ARBGB' OF STRUCTURE <fs> TO <fi>.

   <fi> = 'Z001'.

   ASSIGN COMPONENT 'MSGNR' OF STRUCTURE <fs> TO <fi>.

   <fi> = '001'.

   ASSIGN COMPONENT 'TEXT' OF STRUCTURE <fs> TO <fi>.

   <fi> = 'Testing'.

   e_tab_struc  = <fs>.

From calling program the structure is filled as: