问题
经常做BAPI开发的时候,SAP有个什么DATAX的表对应DATA,每次都要写一堆成对的DATA和DATAX,不然就会修改失败,每次复制粘贴也有点麻烦而且容易漏
代码实现
群里大姑刚好写了段代码用来匹配这个DATA和DATAX格式的,稍微优化了一下封装成function用来适配各种场景
函数代码
FUNCTION zdemo_seele03.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IT_DATA) TYPE ANY TABLE
*" CHANGING
*" REFERENCE(CT_DATAX) TYPE ANY TABLE
*"----------------------------------------------------------------------
FIELD-SYMBOLS: <fs_data> TYPE any,
<fs_cell_data> TYPE any,
<fs_datax> TYPE any,
<fs_cell_datax> TYPE any.
DATA: lr_data TYPE REF TO data,
lr_datax TYPE REF TO data,
lo_str_descr TYPE REF TO cl_abap_structdescr,
lo_strx_descr TYPE REF TO cl_abap_structdescr,
lo_ele_descr TYPE REF TO cl_abap_elemdescr,
lo_elex_descr TYPE REF TO cl_abap_elemdescr,
ls_comp_descr TYPE abap_compdescr.
*&--Get structure of data and datax
CREATE DATA lr_data LIKE LINE OF it_data.
ASSIGN lr_data->* TO <fs_data>.
lo_str_descr ?= cl_abap_structdescr=>describe_by_data( <fs_data> ).
CREATE DATA lr_datax LIKE LINE OF ct_datax.
ASSIGN lr_datax->* TO <fs_datax>.
lo_strx_descr ?= cl_abap_typedescr=>describe_by_data( <fs_datax> ).
*&--Foreach row
LOOP AT it_data ASSIGNING <fs_data>.
" Foreach cell
LOOP AT lo_str_descr->components INTO ls_comp_descr.
ASSIGN COMPONENT ls_comp_descr-name OF STRUCTURE <fs_data> TO <fs_cell_data>.
" check whether data cell field name equals datax cell field name
IF <fs_cell_data> IS NOT INITIAL.
READ TABLE lo_strx_descr->components TRANSPORTING NO FIELDS WITH KEY name = ls_comp_descr-name.
IF sy-subrc = 0.
ASSIGN COMPONENT ls_comp_descr-name OF STRUCTURE <fs_datax> TO <fs_cell_datax>.
lo_elex_descr ?= cl_abap_typedescr=>describe_by_data( <fs_cell_datax> ).
" field name is the same, then check whether element name equals special char1 field( BAPIUPDATE CHAR1CFD_CUSTOM CHANGE_INDICATOR )
* IF lo_elex_descr->absolute_name CS 'BAPIUPDATE' OR
* lo_elex_descr->absolute_name CS 'CHAR1CFD_CUSTOM' OR
* lo_elex_descr->absolute_name CS 'CHANGE_INDICATOR'.
* <fs_cell_datax> = 'X'.
* ELSE.
* <fs_cell_datax> = <fs_cell_data>.
* ENDIF.
" check whether element name is the same
lo_ele_descr ?= cl_abap_typedescr=>describe_by_data( <fs_cell_data> ).
IF lo_ele_descr->absolute_name = lo_elex_descr->absolute_name.
<fs_cell_datax> = <fs_cell_data>.
ELSE.
<fs_cell_datax> = 'X'.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ct_datax = VALUE #( BASE ct_datax
( <fs_datax> ) ).
ENDLOOP.
ENDFUNCTION.
测试场景
*&---------------------------------------------------------------------*
*& Report ZDEMO_SEELE12
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdemo_seele12.
DATA: lt_data TYPE TABLE OF bapimepoitem,
lt_datax TYPE TABLE OF bapimepoitemx.
lt_data = VALUE #( ( po_item = '00010' material = 'M001' )
( po_item = '00020' material = 'M001' ) ).
*lt_datax = VALUE #( ( po_item = '00010' material = 'X' )
* ( po_item = '00020' material = 'X' ) ).
CALL FUNCTION 'ZDEMO_SEELE03'
EXPORTING
it_data = lt_data
CHANGING
ct_datax = lt_datax.
BREAK-POINT.
下面是两位大姑的聊天截图,感恩大姑分享经验和思路~