cancel
Showing results for 
Search instead for 
Did you mean: 

creat ABAP objection dynamiclly

qimingzhennan
Discoverer
0 Kudos

hello, I wonder how to create an ABAP object dynamically, for example, there is a parameter transmit from outer interface which value is '0001' or '0002' or '0003' etc. And then I want to create a data typed 'P0001' or 'P0002' or 'P0003' etc. Btw, these type 'P0001' , 'P0002' , 'P0003' are existed in sap system, they are infoType in module HR . I just want to concatenate 'P' and dynamic parameter, I hope my expression is clear. Thanks for your help!

Accepted Solutions (1)

Accepted Solutions (1)

Rajasekhar_Dina
Participant
0 Kudos

Try this code:

REPORT ytestraj3.

DATA: lv_pnumber(5) TYPE c.

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,
p_tab TYPE ddobjname,
ls_comp LIKE LINE OF lt_comp.


SELECTION-SCREEN: BEGIN OF BLOCK blk.
PARAMETERS: p_num(4) TYPE c OBLIGATORY.
SELECTION-SCREEN: END OF BLOCK blk.

START-OF-SELECTION.

IF NOT p_num IS INITIAL.
CONCATENATE 'P' p_num INTO lv_pnumber.

p_tab = lv_pnumber.

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.
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.

ENDIF.

IF 1 = 2.
ENDIF.


*Note: lo_itab will have the fields of the respective structure under 'KEY'.

capture.jpgcapture2.jpg

qimingzhennan
Discoverer
0 Kudos

It works, Thanks a lot!! And next I have another question, I want to create an inner table with the data in lt_dfies. Using the content of collum 'fieldname', the first row is 'PERNR', the second row is 'INFTY', the third row is 'SUBTY', I hope they can converted to colum1 2 3... I have tried by using assign component, but may be I did in a wrong way so it doesn't work. I'm a new abaper, could you please help me

Answers (0)