Skip to content

Instantly share code, notes, and snippets.

@edegula
Created June 12, 2013 07:07
Show Gist options
  • Save edegula/5763377 to your computer and use it in GitHub Desktop.
Save edegula/5763377 to your computer and use it in GitHub Desktop.
A sample method for create a structure and internal table dynamically using RTTC of RTTI.
*----------------------------------------------------------------
* METHOD create_tab_for_display
* IMPORTING
* edo_table type ref to data
* edo_struc type ref to data.
*----------------------------------------------------------------
method create_tab_for_display.
data:
go_sdescr_new type ref to cl_abap_structdescr,
go_tdescr type ref to cl_abap_tabledescr,
gs_comp type abap_componentdescr,
gt_components type abap_component_tab.
data:
lv_bukrs type bukrs.
* build components
clear gs_comp.
gs_comp-type ?= cl_abap_datadescr=>describe_by_name( 'LFA1-LIFNR' ).
gs_comp-name = 'LIFNR'.
append gs_comp to gt_components.
clear gs_comp.
gs_comp-type ?= cl_abap_datadescr=>describe_by_name( 'CHAR20' ).
gs_comp-name = 'ACTION'.
append gs_comp to gt_components.
loop at gt_bukrs into lv_bukrs.
clear gs_comp.
gs_comp-type ?= cl_abap_datadescr=>describe_by_name( 'ICON_D' ).
concatenate 'BUKRS_' lv_bukrs into gs_comp-name.
append gs_comp to gt_components.
endloop.
clear gs_comp.
gs_comp-type ?= cl_abap_datadescr=>describe_by_name( 'ICON_D' ).
gs_comp-name = 'SUS_ASSIGN'.
append gs_comp to gt_components.
clear gs_comp.
gs_comp-type ?= cl_abap_datadescr=>describe_by_name( 'ICON_D' ).
gs_comp-name = 'PURCH_BLOCK'.
append gs_comp to gt_components.
go_sdescr_new = cl_abap_structdescr=>create( gt_components ).
go_tdescr = cl_abap_tabledescr=>create( go_sdescr_new ).
create data edo_table type handle go_tdescr.
create data edo_struc type handle go_sdescr_new.
endmethod. "create_table_for_display
@Preetharathod123
Copy link

Preetharathod123 commented Mar 8, 2022

Hi,
After creating internal table and structure dynamically with fields of different tables. how do we fetch data into internal table and display the output.

@HavliCzech
Copy link

Hi,
this is how you can further work with dynamically created structure/table. Just an example, combine with coding above.

DATA: lt_result2      TYPE REF TO data,
      ls_result2      TYPE REF TO data.

FIELD-SYMBOLS: <fs_result2> TYPE any,
               <fs_value>   TYPE any.

lo_sdescr = cl_abap_structdescr=>create( lt_components ).
lo_tdescr = cl_abap_tabledescr=>create( lo_sdescr ).
CREATE DATA ls_result2 TYPE HANDLE lo_sdescr.
CREATE DATA lt_result2 TYPE HANDLE lo_tdescr.

ASSIGN ls_result2->* TO <fs_result2>.

" Fill new itab from some existing table
LOOP AT t_result ASSIGNING <fs_result>.

  " fixed fieldname
  ASSIGN COMPONENT 'USERNAME' OF STRUCTURE <fs_result2> TO <fs_value>.
  <fs_value> = <fs_result>-username.

  " dynamic field name 
  ASSIGN COMPONENT <fs_result>-custatrib OF STRUCTURE <fs_result2> TO <fs_value>.
  <fs_value> = abap_true.

ENDLOOP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment