Skip to Content
avatar image
Former Member

How to return back a structure of dynamic tables in a Function Module

Hello Guru,

I'm currently working on a Remote Function Module that should return back a multiple dynamic internal tables.

My FM can generate the dynamic iTables and return them back.

The Problem is that the OUTPUT is not correctly.

How can I define a structure containing a tabname and tablecontent,

Where the tabname is the name of the internal Table like 'MARA' and the tablecontent is containing the data of the Table 'MARA'.

So, this structure should contain a huge number of dynamic tables with their table-data and -name.

We all know that the size of each table must be allocated at the Runtime.

I know how to get the itabs and return them back, but I don't know how to return a structure like this.

Where each table muss be allocated separatly.

And the mother's table must be a structure.

Node 0

-> Node 1 : 'T001W'

-> Table of 'T001W'

-> Node 2 : 'MARA'

-> Table of 'MARA'

....

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Sep 30, 2008 at 01:54 AM

    Hello Kais

    I would recommend to use XML to retrieve the data from the remote system. The RFC-fm takes a table name as input and should return all table entries on the remote system.

    DATA: gd_xstring     TYPE xstring,
               gd_tabname  TYPE tabname.
    
    DATA: ldo_data   TYPE REF TO data.
    FIELD-SYMBOLS: <lt_itab>    TYPE TABLE.
    
      gd_tabname = 'MARA'.
    
      CALL FUNCTION 'Z_MY_RFC_FUNC'
        DESTINATION '<rfc_destination>'
        IMPORTING
          id_tabname = gd_tabname
        EXPORTING
          ed_xstring  = gd_xstring
        EXCEPTIONS
          ...
    
    
    CREATE DATA ldo_data TYPE TABLE OF (gd_tabname).
    ASSIGN ldo_data->* TO <lt_itab>.
    
    
    " Convert xstring back into XML
      CALL TRANSFORMATION id_ident
        SOURCE XML gd_xstring
        RESULT itab = <lt_itab>. 
    

    Within the RFC-fm you dynamically create your itab and do the selection:

    FUNCTION  'Z_MY_RFC_FUNC'.
    
    DATA: ldo_data   TYPE REF TO data.
    FIELD-SYMBOLS: <lt_itab>    TYPE TABLE.
    
    CREATE DATA ldo_data TYPE TABLE OF (id_tabname).
    ASSIGN ldo_data->* TO <lt_itab>.
    
    SELECT * FROM (id_tabname) INTO TABLE <lt_itab>.
    
    CALL TRANSFORMATION id_ident
      SOURCE itab = <lt_itab>
      RESULT XML ed_xstring.
    
    ENDFUNCTION.
    

    The advantage of using xstring (instead of string) is that you exclude any problems with Unicde this way.

    Regards

    Uwe

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Uwe,

      Thanks for your respond.

      The call TRANSFORMATION has raised an exception called CX_INVALID_TRANSFORMATION.

      Is there any other possibility instead of XML.

      The FM that I'm building will be called by an external program implemented with JAVA.

      So, with the help of the JCO and some others features, I'll get the tables and the fields.

      Using XML-Files will gone complicate this many times.

      My FM is getting a list of BAPI-elements and their fields checking for the check-tables related to the fieldselements and returning the tables back.

      This FM is designed to multiple check-tables which will be retrieved and returned back.

      Later it will be used by the external Program, the programmer of the external Program will call my FM and wont to get many check-tables which he will then display or do some others works with it.

      Thanks a lot.

      Regards,

      Kais

      Here how it looks like

      -

      -


      FUNCTION z_**_******_**_check_table .
      *"----------------------------------------------------------------------
      *"*"Lokale Schnittstelle:
      *"  IMPORTING
      *"     REFERENCE(GET_CT_FLAG) LIKE  BAPIFLAG-BAPIFLAG DEFAULT SPACE
      *"  EXPORTING
      *"     VALUE(VERSION_ID) TYPE  Z***_VERSION_ID
      *"  TABLES
      *"      FIND_CHECKTABLES STRUCTURE  Z***_*****_FND_CHECKTAB
      *"      RETURN_CHECKTABLES STRUCTURE  Z***_*****_RTN_CHECKTAB
      *"      DYNTAB
      *"      RETURN STRUCTURE  BAPIRET2
      *"----------------------------------------------------------------------
      ************************************************************************
        MOVE 'V 1.0' TO version_id.
      *
      *              V 1.0
      *
      *  history table
      *
      ************************************************************************
      
      *&------------------------------------------------------------&*
      *&------------------------------------------------------------&*
      *& MAPPING                                                    &*
      *&    BAPI-Structures field-elements to real component-types. &*
      *&------------------------------------------------------------&*
      *& FINDING                                                    &*
      *&    Check Tables related to a given field-element           &*
      *&    of a given BAPI-Structure.                              &*
      *&------------------------------------------------------------&*
      *& GENERATING                                                 &*
      *&    dynamic check tables (prüftabellen) structures          &*
      *& and PUSHING in it the real needed data.                    &*
      *&------------------------------------------------------------&*
      *& RETURNING                                                  &*
      *&    the given structure back.                               &*
      *&------------------------------------------------------------&*
      *&------------------------------------------------------------&*
      
        DATA: w_ret_checktab LIKE TABLE OF z***_********_rtn_checktab,
              l_fnd_checktab LIKE TABLE OF z***_*******_fnd_checktab WITH HEADER LINE,
              l_ret_checktab LIKE LINE OF w_ret_checktab.
      
      
        DATA par1 LIKE sy-msgv1.
        DATA par2 LIKE sy-msgv2.
      
        DATA : p_dyntab_ref TYPE REF TO data.
      
        DATA: BEGIN OF check_table OCCURS 0,
                entitytab TYPE entitytab,
              END OF check_table.
      
        DATA: ct_table LIKE TABLE OF check_table WITH HEADER LINE,
              l_ct_table LIKE LINE OF ct_table.
      
      
        FIELD-SYMBOLS: <p_dyntab> TYPE ANY TABLE.
      
      *First Step : MAPPING AND FINDING
        LOOP AT find_checktables INTO l_fnd_checktab.
      
          CLEAR w_ret_checktab.
      
      
          SELECT SINGLE d~tabname d~fieldname e~rollname e~domname e~entitytab
          INTO CORRESPONDING FIELDS OF l_ret_checktab
          FROM ( dd03l AS d
                 INNER JOIN dd04l AS e ON d~rollname = e~rollname
                                      AND d~domname  = e~domname )
          WHERE d~tabname = l_fnd_checktab-tabname AND
                d~fieldname = l_fnd_checktab-fieldname.
      
      
          IF sy-subrc <> 0.
            MOVE l_fnd_checktab-tabname TO par1.
            MOVE l_fnd_checktab-fieldname TO par2.
      
            CALL FUNCTION 'BALW_BAPIRETURN_GET2'
              EXPORTING
                type   = 'E'
                cl     = 'Z***'
                number = '000'
                par1   = par1
                par2   = par2
              IMPORTING
                return = return.
            APPEND return.
          ELSE.
            MOVE-CORRESPONDING l_ret_checktab TO return_checktables.
            APPEND return_checktables.
            MOVE-CORRESPONDING l_ret_checktab TO ct_table.
            APPEND ct_table.
      
          ENDIF.
      
        ENDLOOP.
      
      *Second Step : Generating, Pushing and Returning
      
        IF get_ct_flag EQ 'X'.
      
          LOOP AT ct_table INTO l_ct_table.
      
      * Work todo : return the dynamic tables correctly
      * i.e. dyntab should have a structure contening tablename and tablecontent
      * every check table should have its own line on the mother's table
      * mother's table : dyntab
      *   -> tablename      -> tablecontent
      *      T001W            -------------------------------
      *                       -  check table T001W content  -
      *                       -------------------------------
      *      T006           -> tablecontent
      *                       -------------------------------
      *                       -  check table T006 content   -
      *                       -------------------------------
      *      ...
      
            CREATE DATA p_dyntab_ref LIKE dyntab[].
            ASSIGN p_dyntab_ref->* TO <p_dyntab>.
      
      *      CREATE DATA p_dyntab_ref TYPE STANDARD TABLE OF (l_ct_table-entitytab).
      *      ASSIGN p_dyntab_ref->* TO <p_dyntab>.
      
            PERFORM dynamic_table_creation
              USING l_ct_table-entitytab
                    <p_dyntab>.
      
            LOOP AT <p_dyntab> INTO dyntab.
              APPEND dyntab TO dyntab[].
            ENDLOOP.
      
          ENDLOOP.
      
        ENDIF.
      
      ENDFUNCTION.
      
      *&---------------------------------------------------------------------*
      *&      Form  dynamic_table_creation
      *&---------------------------------------------------------------------*
      * DYNAMIC CHECK-TABLES :
      *       FINDING
      *       CREATING
      *       FILLING
      *  AND  RETURING
      *----------------------------------------------------------------------*
      *      -->P_TABNAME  text
      *----------------------------------------------------------------------*
      FORM dynamic_table_creation
        USING
          p_tname TYPE entitytab
        CHANGING
           p_dyntab TYPE table.
      
        DATA : go_sdescr TYPE REF TO cl_abap_structdescr,
               go_ydescr TYPE REF TO cl_abap_typedescr,
               go_tdescr TYPE REF TO cl_abap_tabledescr.
      
        DATA : gt_compdescrs TYPE abap_compdescr_tab,
               gs_compd TYPE abap_compdescr,
               gt_components TYPE abap_component_tab,
               gs_comp TYPE abap_componentdescr.
      
        DATA : gdo_handle TYPE REF TO data,
               gd_tabfield TYPE string.
      
        FIELD-SYMBOLS:
               <ct_itab> TYPE ANY TABLE.
      
      *&-------------------------------------------------
      *&        FINDING THE CT-TABLE                    -
      *&-------------------------------------------------
        CALL METHOD cl_abap_structdescr=>describe_by_name
          EXPORTING
            p_name         = p_tname
          RECEIVING
            p_descr_ref    = go_ydescr
          EXCEPTIONS
            type_not_found = 1
            OTHERS         = 2.
      
        go_sdescr ?= go_ydescr.
      
        gt_compdescrs = go_sdescr->components.
      
      *&------------------------------------------------------------------------------------------------------------
        LOOP AT gt_compdescrs INTO gs_compd.
      
          CONCATENATE p_tname gs_compd-name INTO gd_tabfield
                                               SEPARATED BY '-'.
      
          gs_comp-name = gs_compd-name.
      
          gs_comp-type ?= cl_abap_typedescr=>describe_by_name( gd_tabfield ).
      
          INSERT gs_comp INTO TABLE gt_components.
      
        ENDLOOP.
      *&------------------------------------------------------------------------------------------------------------
      
        IF gt_components IS NOT INITIAL.
      *&-------------------------------------------------
      *&        CREATING THE CT-TABLE                   -
      *&-------------------------------------------------
          go_sdescr = cl_abap_structdescr=>create( gt_components ).
          go_tdescr = cl_abap_tabledescr=>create( go_sdescr ).
      
          CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
          ASSIGN gdo_handle->* TO <ct_itab>.
      
      *&-------------------------------------------------
      *&        FILLING THE CT-TABLE                    -
      *&-------------------------------------------------
          SELECT *
          FROM (p_tname)
          INTO CORRESPONDING FIELDS OF TABLE <ct_itab>.
      *&-------------------------------------------------
        ELSE.
      *FAILURE OR TABLE EMPTY
        ENDIF.
      *&-------------------------------------------------
      *&        RETURNING THE CT-TABLE                  -
      *&-------------------------------------------------
        p_dyntab = <ct_itab>.
      *&-------------------------------------------------
      
      ENDFORM.                    "dynamic_table_creation

  • avatar image
    Former Member
    Oct 04, 2008 at 03:55 AM

    Hello Everybody,

    Please help Me.

    I'm trying to find a way to get as OUTPUT a dynamic Table from a Function Module.

    I declared a table called dyntab without a type.

    And this table should have a dynamic table back.

    The problem that I get the data back but, I only found a data without any field-elements.

    The data are there but there's no structure, no table.

    How can I find a way to get a correct dynamic table back?

    Best Regards,

    Kais

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Kais

      The syntax error was due to a wrong transformation (id_ident should have been ID_INDENT). Below you see a simple sample report demonstrating how easy it is to work with XML.

      *&---------------------------------------------------------------------*
      *& Report  ZUS_SDN_TABLE_READ_VIA_RFC_XML
      *&
      *&---------------------------------------------------------------------*
      *& Thread: How to return back a structure of dynamic tables in a Function Module
      *& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1067568"></a>
      *&---------------------------------------------------------------------*
      
      REPORT  ZUS_SDN_TABLE_READ_VIA_RFC_XML.
      
      
      
      data: gdo_data   type ref to data,
            gd_xstring type xstring.
      
      
      field-symbols:
        <gt_itab>    type table.
      
      PARAMETERs:
        p_tabnam    type tabname  default 'T001W'.
      
      
      START-OF-SELECTION.
      
      
        CALL FUNCTION 'ZUS_SDN_TABLE_READ_VIA_RFC'
          DESTINATION 'NONE'   " simulate RFC call
          EXPORTING
            id_tabname                  = p_tabnam
          IMPORTING
            ED_XSTRING                  = gd_xstring
          EXCEPTIONS
            SYSTEM_FAILURE              = 1
            COMMUNICATION_FAILURE       = 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.
      
      
      CREATE DATA gdo_data TYPE TABLE OF (p_tabnam).
      ASSIGN gdo_data->* TO <gt_itab>.
      
      
      " Convert xstring back into XML
        CALL TRANSFORMATION id_indent
          SOURCE XML gd_xstring
          RESULT itab = <gt_itab>.
      
      
        CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
          EXPORTING
            I_STRUCTURE_NAME                  = p_tabnam
      *     IS_LAYOUT_LVC                     =
      *     IT_FIELDCAT_LVC                   =
      *     I_DEFAULT                         = 'X'
      *     I_SAVE                            = ' '
      *     IS_VARIANT                        =
          TABLES
            t_outtab                          = <gt_itab>
          EXCEPTIONS
            PROGRAM_ERROR                     = 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.
      
      
      end-of-selection.
      

      FUNCTION zus_sdn_table_read_via_rfc.
      *"----------------------------------------------------------------------
      *"*"Local Interface:
      *"  IMPORTING
      *"     VALUE(ID_TABNAME) TYPE  TABNAME
      *"  EXPORTING
      *"     VALUE(ED_XSTRING) TYPE  XSTRING
      *"  EXCEPTIONS
      *"      SYSTEM_FAILURE
      *"      COMMUNICATION_FAILURE
      *"      ERROR
      *"----------------------------------------------------------------------
      
        DATA: ldo_data   TYPE REF TO data.
        FIELD-SYMBOLS: <lt_itab>    TYPE table.
      
        CREATE DATA ldo_data TYPE TABLE OF (id_tabname).
        ASSIGN ldo_data->* TO <lt_itab>.
      
        SELECT * FROM (id_tabname)
          INTO CORRESPONDING FIELDS OF TABLE <lt_itab>.
      
        CALL TRANSFORMATION id_indent
          SOURCE itab = <lt_itab>
          RESULT XML ed_xstring.
      
      
      ENDFUNCTION.
      

      Regards

      Uwe