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

Dynamic structure creation in SAP ABAP using classes

0 Likes
9,624

Hi Experts,

I need to create dynamic structure and internal table to append the records to the corresponding internal table. I am using get( ) from class CL_ABAP_STRUCTDESCR. But not getting the required result.

Can anyone suggest what should be the approach for the same.

Thanks and Regards,

Aditya Mane

1 ACCEPTED SOLUTION
Read only

emanuel_klenner
Active Participant
0 Likes
9,008

Here is an example for dynamically creating a work area and internal table and then using that for a dynamic SELECT. I hope it helps.

REPORT zema_dynamic_itab.

DATA:
lo_itab TYPE REF TO cl_abap_tabledescr,
lo_struc TYPE REF TO cl_abap_structdescr,
lo_data TYPE REF TO cl_abap_datadescr,
lo_type TYPE REF TO cl_abap_typedescr,
gt_keys TYPE string_table.

DATA: lt_dfies TYPE TABLE OF dfies,
ls_dfies LIKE LINE OF lt_dfies.

DATA: lt_comp TYPE abap_component_tab,
ls_comp LIKE LINE OF lt_comp.

PARAMETERS:
p_tab TYPE ddobjname DEFAULT 'ICLITEM'.

START-OF-SELECTION.
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
tabname = p_tab
TABLES
dfies_tab = lt_dfies
EXCEPTIONS
not_found = 1
OTHERS = 2.

LOOP AT lt_dfies INTO ls_dfies
WHERE keyflag IS NOT INITIAL.
APPEND ls_dfies-fieldname TO gt_keys.

ls_comp-name = ls_dfies-fieldname.

CALL METHOD cl_abap_datadescr=>describe_by_name
EXPORTING
p_name = ls_dfies-rollname
RECEIVING
p_descr_ref = lo_type
EXCEPTIONS
type_not_found = 1
OTHERS = 2.

ls_comp-type ?= lo_type.

APPEND ls_comp TO lt_comp.
ENDLOOP.

TRY.
CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = lt_comp
RECEIVING
p_result = lo_struc.

CATCH cx_sy_struct_creation .
ENDTRY.

TRY.
* Create called multiple times for the same structure can cause memory problems. Use get method instead
CALL METHOD cl_abap_tabledescr=>get
EXPORTING
p_line_type = lo_struc
RECEIVING
p_result = lo_itab.

CATCH cx_sy_table_creation .
ENDTRY.

BREAK-POINT.

DATA: lo_tab TYPE REF TO data.
FIELD-SYMBOLS: <lt_tab> TYPE table.

CREATE DATA lo_tab TYPE HANDLE lo_itab.
ASSIGN lo_tab->* TO <lt_tab>.

SELECT (gt_keys) INTO TABLE <lt_tab>
FROM (p_tab) UP TO 100 ROWS.

  BREAK-POINT.
4 REPLIES 4
Read only

FredericGirod
Active Contributor
9,008

Could you post your code ? (using the [CODE] button)

Read only

Sandra_Rossi
Active Contributor
0 Likes
9,008

Your code is missing

+

What result do you get and what do you expect?

Read only

emanuel_klenner
Active Participant
0 Likes
9,009

Here is an example for dynamically creating a work area and internal table and then using that for a dynamic SELECT. I hope it helps.

REPORT zema_dynamic_itab.

DATA:
lo_itab TYPE REF TO cl_abap_tabledescr,
lo_struc TYPE REF TO cl_abap_structdescr,
lo_data TYPE REF TO cl_abap_datadescr,
lo_type TYPE REF TO cl_abap_typedescr,
gt_keys TYPE string_table.

DATA: lt_dfies TYPE TABLE OF dfies,
ls_dfies LIKE LINE OF lt_dfies.

DATA: lt_comp TYPE abap_component_tab,
ls_comp LIKE LINE OF lt_comp.

PARAMETERS:
p_tab TYPE ddobjname DEFAULT 'ICLITEM'.

START-OF-SELECTION.
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
tabname = p_tab
TABLES
dfies_tab = lt_dfies
EXCEPTIONS
not_found = 1
OTHERS = 2.

LOOP AT lt_dfies INTO ls_dfies
WHERE keyflag IS NOT INITIAL.
APPEND ls_dfies-fieldname TO gt_keys.

ls_comp-name = ls_dfies-fieldname.

CALL METHOD cl_abap_datadescr=>describe_by_name
EXPORTING
p_name = ls_dfies-rollname
RECEIVING
p_descr_ref = lo_type
EXCEPTIONS
type_not_found = 1
OTHERS = 2.

ls_comp-type ?= lo_type.

APPEND ls_comp TO lt_comp.
ENDLOOP.

TRY.
CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = lt_comp
RECEIVING
p_result = lo_struc.

CATCH cx_sy_struct_creation .
ENDTRY.

TRY.
* Create called multiple times for the same structure can cause memory problems. Use get method instead
CALL METHOD cl_abap_tabledescr=>get
EXPORTING
p_line_type = lo_struc
RECEIVING
p_result = lo_itab.

CATCH cx_sy_table_creation .
ENDTRY.

BREAK-POINT.

DATA: lo_tab TYPE REF TO data.
FIELD-SYMBOLS: <lt_tab> TYPE table.

CREATE DATA lo_tab TYPE HANDLE lo_itab.
ASSIGN lo_tab->* TO <lt_tab>.

SELECT (gt_keys) INTO TABLE <lt_tab>
FROM (p_tab) UP TO 100 ROWS.

  BREAK-POINT.
Read only

RaymondGiuseppi
Active Contributor
0 Likes
9,008

Did you perform a simple search in forum such as "CL_ABAP_STRUCTDESCR" in wiki part, you should