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

Abap internal table declaration.

former_member197870
Participant
0 Likes
3,536

I am in program A and calls a routine in program B (in this case ZMOD_EMAIL_REPORT TABLES) like

PERFORM SEND_EMAIL IN PROGRAM ZMOD_EMAIL_REPORT TABLES it_alvt using SITID TITLE excel_heading attch_subject .

Suppose it_alvt in program A has data type sequence as

F1 TYPE kna1-kunnr,

F2 TYPE knvv-vkorg,

F3 type vbrk-wrbtr.

I want that in program B i must be able to dynamically declare an internal table with reference to internal table in program A

F1(20) TYPE C,

F2(20) TYPE C,

F3(20) TYPE C.

The field declaration should happen with exact field name but only difference being maintaining char datatypes.

16 REPLIES 16
Read only

SimoneMilesi
Active Contributor
3,112

The question is.. why?
Really, what's the final goal? Reusing the code? what?

Read only

juan_suros
Contributor
0 Likes
3,112

This will work. Not sure this is a good idea.

FORM send_email TABLES it_alvt TYPE STANDARD TABLE
                 USING sitid
                       title
                       excel_heading
                       attch_subject.


  DATA:
    tab_ref    TYPE REF TO cl_abap_tabledescr,
    struc_ref  TYPE REF TO cl_abap_structdescr,
    lt_columns TYPE lvc_t_fcat,
    lv_column  TYPE lvc_s_fcat,
    t_data     TYPE STANDARD TABLE OF vbap,
    type_alv   TYPE REF TO data,
    type_row   TYPE REF TO data.


  FIELD-SYMBOLS: <alv>         TYPE STANDARD TABLE.
  FIELD-SYMBOLS: <dynamic_row> TYPE any.
  FIELD-SYMBOLS: <field>       TYPE any.
  FIELD-SYMBOLS: <f>           TYPE abap_compdescr.


* Reference internal table definition
  tab_ref ?= cl_abap_tabledescr=>describe_by_data( it_alvt[] ).
  struc_ref ?= tab_ref->get_table_line_type( ).


* For each field of internal table...
  LOOP AT struc_ref->components ASSIGNING <f>.

    CLEAR: lv_column.

    lv_column-fieldname = <f>-name.
    lv_column-tabname   = 'T_DATA'.
    lv_column-col_pos   = sy-tabix.
    lv_column-outputlen = <f>-length.
    lv_column-datatype  = 'CHAR'.

*   Add a field with matching definition to dynamic table definition
    APPEND lv_column TO lt_columns.

  ENDLOOP.


* Create dynamic table
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = lt_columns
    IMPORTING
      ep_table                  = type_alv
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.
  IF sy-subrc EQ 0.
    ASSIGN type_alv->* TO <alv>.              "Get reference to dynamic table
    CREATE DATA type_row LIKE LINE OF <alv>.  "Create work area for dynamic table
    ASSIGN type_row->* TO <dynamic_row>.      "Get reference to work area of dynamic table
  ENDIF.


* Do this to add rows to dynamic table
  APPEND <dynamic_row> TO <alv>.


* Do this to use rows and fields of dynamic table
  LOOP AT <alv> ASSIGNING <dynamic_row>.

*    <dynamic_row>-vbeln = 'a'.  "<== THIS DOES NOT WORK !!!

    ASSIGN COMPONENT 'F1' OF STRUCTURE <dynamic_row> TO <field>.
    <field> = 'this works'.

  ENDLOOP.

ENDFORM.


Read only

matt
Active Contributor
0 Likes
3,112
cl_alv_table_create=>create_dynamic_table

Really? It was a useful trick in the past, but it shouldn't be part of modern code. With RTTS you don't need to use this.

Read only

0 Likes
3,112

I was expressing doubt as to the design. Not relevant for this sort of message board, I suppose.

Read only

juan_suros
Contributor
0 Likes
3,112

Forgot to remove the T_DATA reference. Also, you will probably want this bit:

* Copy content of passed table to new iTab
  LOOP AT it_alvt ASSIGNING FIELD-SYMBOL(<this>).
    MOVE-CORRESPONDING <this> TO <dynamic_row>.
    APPEND <dynamic_row> TO <alv>.
  ENDLOOP.


Read only

matt
Active Contributor
3,112

Please use comments to comment on answers - don't add another answer.

Read only

DoanManhQuynh
Active Contributor
3,112

you should move the common code into function module or class, it would be better. to dynamic table structure you can use RTTS, read more here:

https://blogs.sap.com/2014/04/03/create-dynamic-table-using-rtts-and-display-in-alv/

Read only

former_member197870
Participant
0 Likes
3,112

Thanks for your great support.I will give this a try and post the final code.

Read only

0 Likes
3,112

Please use comments to comment on answers - don't add another answer.

If you're using Juan Suros' answer, be aware that he's using cl_alv_table_create=>create_dynamic_table and that's really not best practice.

Read only

former_member197870
Participant
0 Likes
3,111

Thank you so much !. I guessed the latter part.

Read only

0 Likes
3,111

Please use comments to comment on answers - don't add another answer.

Read only

3,111

matthew.billingham is it possible to ask the SCN team to name the button "Propose your Solution" instead of "Answer"? because many people think that "answer" means "reply to someone" (which is right, but only outside SCN).

Read only

0 Likes
3,111

Good idea Sandra - you should propose it to the powers that be.

Read only

former_member197870
Participant
0 Likes
3,111

Whats the flaw Mathew ?.What can be done alternate to it ?

Read only

0 Likes
3,111

As Quynh Doan Manh said.

It would be better. to ... use RTTS, read more here: https://blogs.sap.com/2014/04/03/create-dynamic-table-using-rtts-and-display-in-alv/

RTTS is the proper way to do it. cl_alv_table_create=>create_dynamic_table generates a subroutine pool, so there's a limit on how many tables you can create and it wasn't really designed for general use. But the main reason is that RTTS is worth learning as it is very powerful.

Read only

former_member197870
Participant
0 Likes
3,111

Great stuff.Thanks.