abap 动态生成屏幕_如何用代码动态生成ABAP类型

403c2064282e7acd87dc1b29dead2b0b.png

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:

872191783b307596b0945e7398376eda.png
Figure1: an example of data type definition

which needs to be generate in the runtime

The variable could have the component named “PRODUCTS” which is a table type, the table line type is a structure with five fields: PROD_ID, PROD_TXT, VALID_FROM, VALID_TO and COMPONENTS. The last field COMPONENTS is again a table type, with the table line type consisting of four fields COMP_ID, COMP_TXT, OPERATOR and again a table type for last field NOTES.

The row in the picture above marked with green seems a little strange and not necessary at a first glance, but it really makes sense. Consider the setting for table type in ABAP Dictionary.

6de88c2c74715ba8833116da57c656cf.png

How to use the RTTC tool

I write a simple report to create the data type and define a variable with that type using the RTTC tool zcl_rttc_tool( source code in attachment ). To use the tool, you just need to put into the definition of the data type to be generated, and the exporting parameter er_ref contains the reference pointing to the variable with the very data type.

538c4020d997a676b9f658364b5ee709.png

To facilitate the filling of importing it_des_tab, I create three simple macro in report:

DEFINE define_table_type.
APPEND INITIAL LINE TO lt_definition ASSIGNING <type>.
  <type> = VALUE #( level = &1 field = &2 kind = ZCL_RTTC_TOOL=>c_table parent = &3 ).
END-OF-DEFINITION.
DEFINE define_tab_line_type.
  APPEND INITIAL LINE TO lt_definition ASSIGNING <type>.
 <type> = VALUE #( level = &1 kind = ZCL_RTTC_TOOL=>c_structure parent = &2 ).
END-OF-DEFINITION.
DEFINE define_element.
APPEND INITIAL LINE TO lt_definition ASSIGNING <type>.
  <type> = VALUE #( level = &1 field = &2 kind = ZCL_RTTC_TOOL=>c_element type = &3 parent = &4 ).
END-OF-DEFINITION.

1ce20cc28599282b84d60b7ab5ba54af.png

How to access the variable created by RTTC tool

We get the variable from tool which is a reference points to a structure which has a transient technical type. The type is only valid withing current session.

c2837171c304372b45f4a5c811e78c95.png

Although you can find the component PRODUCTS in debugger, you cannot use lr_data->products to access it in your code, since the structure is not known by compiler in design time.

0b0895e62c17ae36d75ade09cb4a1eb0.png

Instead, you have to access it via so called dynamic programming via field symbol:

FIELD-SYMBOLS: <data>     TYPE any,
               <prod_tab> TYPE STANDARD TABLE,
               <comp_tab> TYPE STANDARD TABLE,
               <note_tab> TYPE STANDARD TABLE.
ASSIGN lr_data->* TO <data>.
CHECK sy-subrc = 0.
ASSIGN COMPONENT 'PRODUCTS' OF STRUCTURE <data> TO <prod_tab>.
CHECK sy-subrc = 0.
" create a new empty line to component PRODUCTS
APPEND INITIAL LINE TO <prod_tab> ASSIGNING FIELD-SYMBOL(<prod_line>).
CHECK sy-subrc = 0.
"fill the field PROD_ID for the first table line
ASSIGN COMPONENT 'PROD_ID' OF STRUCTURE <prod_line> TO FIELD-SYMBOL(<prod_id>).
CHECK sy-subrc = 0.
<prod_id> = 'MCF-0001'.
ASSIGN COMPONENT 'COMPONENTS' OF STRUCTURE <prod_line> TO <comp_tab>.
CHECK sy-subrc = 0.
" create a new empty line to component COMPONENTS
APPEND INITIAL LINE TO <comp_tab> ASSIGNING FIELD-SYMBOL(<comp_line>).
CHECK sy-subrc = 0.
" fill COMP_ID for the first table line
ASSIGN COMPONENT 'COMP_ID' OF STRUCTURE <comp_line> TO FIELD-SYMBOL(<comp_id>).
CHECK sy-subrc = 0.
<comp_id> = 'COMP_0001'.
ASSIGN COMPONENT 'NOTES' OF STRUCTURE <comp_line> TO <note_tab>.
CHECK sy-subrc = 0.
" create a new empty line to component NOTES
APPEND INITIAL LINE TO <note_tab> ASSIGNING FIELD-SYMBOL(<note_line>).
CHECK sy-subrc = 0.
" fill NOTE_ID for the first table line
ASSIGN COMPONENT 'NOTE_ID' OF STRUCTURE <note_line> TO FIELD-SYMBOL(<note_id>).
CHECK sy-subrc = 0.
<note_id> = 'NOTE_0001'.

the filled data is displayed in the debugger as below, let’s compare it with figure 1:

59d19ea8f0180c90b8029933a9029b58.png

872191783b307596b0945e7398376eda.png

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

31d4f1d9f8fbcc0a43849cd30aebff16.png
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值