天天看点

如何用代码动态生成ABAP类型

In one of my project the data type of the variable to hold service consumption result is not known in design time so I have to generate the data type dynamically via code, using ABAP RTTC( runtime type creation ). For detail of RTTC and dynamic programming, please refer to sap help.

This blog will introduce a handy RTTC tool to generate any kind of complex data type in the runtime and demonstrate how to access the variable with that data type dynamically using field symbol.

one example: I need to define a variable with the following data type. The structure of the data type is only known in the runtime:

如何用代码动态生成ABAP类型
如何用代码动态生成ABAP类型
如何用代码动态生成ABAP类型
如何用代码动态生成ABAP类型
如何用代码动态生成ABAP类型
如何用代码动态生成ABAP类型

FIELD-SYMBOLS:     TYPE any,

              TYPE STANDARD TABLE,

              TYPE STANDARD TABLE.

ASSIGN lr_data->* TO .

CHECK sy-subrc = 0.

ASSIGN COMPONENT 'PRODUCTS' OF STRUCTURE TO .

" create a new empty line to component PRODUCTS

APPEND INITIAL LINE TO ASSIGNING FIELD-SYMBOL().

"fill the field PROD_ID for the first table line

ASSIGN COMPONENT 'PROD_ID' OF STRUCTURE TO FIELD-SYMBOL().

= 'MCF-0001'.

ASSIGN COMPONENT 'COMPONENTS' OF STRUCTURE TO .

" create a new empty line to component COMPONENTS

" fill COMP_ID for the first table line

ASSIGN COMPONENT 'COMP_ID' OF STRUCTURE TO FIELD-SYMBOL().

= 'COMP_0001'.

ASSIGN COMPONENT 'NOTES' OF STRUCTURE TO .

" create a new empty line to component NOTES

" fill NOTE_ID for the first table line

ASSIGN COMPONENT 'NOTE_ID' OF STRUCTURE TO FIELD-SYMBOL().

= 'NOTE_0001'.

如何用代码动态生成ABAP类型
如何用代码动态生成ABAP类型

继续阅读