‎2018 Oct 10 1:42 PM
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.
‎2018 Oct 10 4:21 PM
The question is.. why?
Really, what's the final goal? Reusing the code? what?
‎2018 Oct 10 7:26 PM
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.
‎2018 Oct 11 8:38 AM
cl_alv_table_create=>create_dynamic_tableReally? 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.
‎2018 Oct 11 10:49 PM
I was expressing doubt as to the design. Not relevant for this sort of message board, I suppose.
‎2018 Oct 10 7:41 PM
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.
‎2018 Oct 16 10:58 AM
Please use comments to comment on answers - don't add another answer.
‎2018 Oct 11 4:05 AM
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/
‎2018 Oct 11 6:22 AM
Thanks for your great support.I will give this a try and post the final code.
‎2018 Oct 11 8:39 AM
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.
‎2018 Oct 16 10:40 AM
‎2018 Oct 16 10:58 AM
Please use comments to comment on answers - don't add another answer.
‎2018 Oct 16 6:56 PM
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).
‎2018 Oct 19 3:14 PM
Good idea Sandra - you should propose it to the powers that be.
‎2018 Oct 23 11:46 AM
‎2018 Oct 23 12:30 PM
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.
‎2018 Oct 25 1:11 PM