2013 Oct 25 10:41 AM
Hello experts,
i am using the FM BAPI_MATERIAL_SAVEDATA to create material masters.
It works good but I want to dynamically fill the X- Structures such as BAPI_MARAX and BAPI_MARCX
with the 'X' value where the field should be updated/ created in dependence of the filled values in BAPI_MARA and so on.
Can you please provide a good solution or Function where i have to pass the local structure
or only the name of the structure to be updated (ie. BAPI_MARA/ X)?
Any help will be appreciated,
Thanks a lot!
2013 Oct 25 10:55 AM
Hi
The bapi structure with data and corrisponding structure X have field with the same name and (I believe the same position), so you can use the field-symbols:
A) If the fields are in the same position:
FIELD-SYMBOLS: <FS_BAPI> TYPE ANY,
<FS_BAPI> TYPE ANY.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE BAPI_MARA TO <FS_BAPI>.
IF SY-SUBRC <> 0. EXIT. ENDIF.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE BAPI_MARAX TO <FS_BAPIX>.
IF <FS_BAPI> IS NOT INITIAL.
<FS_BAPIX> = 'X'.
ENDIF.
ENDDO.
If the position is not the same you can use the name, so you can use fm DDIF_FIELDINFO_GET in order to get the name of a structure, the result will be in DFIES_TAB:
LOOP AT DFIES_TAB..
ASSIGN COMPONENT DFIES_TAB-FIELDNAME OF STRUCTURE BAPI_MARA
TO <FS_BAPI>.
ASSIGN COMPONENT DFIES_TAB-FIELDNAME OF STRUCTURE BAPI_MARAX
TO <FS_BAPIX>.
IF <FS_BAPI> IS NOT INITIAL.
<FS_BAPIX> = 'X'.
ENDIF.
ENDLOOP.
Max
2013 Oct 25 10:48 AM
Simply when you are filling BAPI_MARA then you update the flag also..? Are you doing some move-corresponding stuff../?
2013 Oct 25 12:35 PM
Hi nabheet,
yes, im moving the data from the structures of FM BAPI_MATERIAL_GET_ALL to the _SAVEDATA structures. Therefore i looked for a dynamic solution.
thanks!
2013 Oct 25 10:55 AM
Hi
The bapi structure with data and corrisponding structure X have field with the same name and (I believe the same position), so you can use the field-symbols:
A) If the fields are in the same position:
FIELD-SYMBOLS: <FS_BAPI> TYPE ANY,
<FS_BAPI> TYPE ANY.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE BAPI_MARA TO <FS_BAPI>.
IF SY-SUBRC <> 0. EXIT. ENDIF.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE BAPI_MARAX TO <FS_BAPIX>.
IF <FS_BAPI> IS NOT INITIAL.
<FS_BAPIX> = 'X'.
ENDIF.
ENDDO.
If the position is not the same you can use the name, so you can use fm DDIF_FIELDINFO_GET in order to get the name of a structure, the result will be in DFIES_TAB:
LOOP AT DFIES_TAB..
ASSIGN COMPONENT DFIES_TAB-FIELDNAME OF STRUCTURE BAPI_MARA
TO <FS_BAPI>.
ASSIGN COMPONENT DFIES_TAB-FIELDNAME OF STRUCTURE BAPI_MARAX
TO <FS_BAPIX>.
IF <FS_BAPI> IS NOT INITIAL.
<FS_BAPIX> = 'X'.
ENDIF.
ENDLOOP.
Max
2013 Oct 25 11:41 AM
2015 Oct 14 11:30 AM
otherwise get the field names of BAPIMARAX separately using DDIF_FIELDINFO_GET into another table. And in the loop do a Read with key = fieldname.
LOOP AT lt_dfies_1 INTO wa_dfies_1.
ASSIGN COMPONENT wa_dfies_1-fieldname OF STRUCTURE ls_order_header_in TO <fs_bapi>.
READ TABLE lt_dfies_2 INTO wa_dfies_2 WITH KEY fieldname = wa_dfies_1-fieldname.
IF sy-subrc IS INITIAL.
ASSIGN COMPONENT wa_dfies_1-fieldname OF STRUCTURE ls_order_header_inx TO <fs_bapix>.
IF <fs_bapi> IS NOT INITIAL.
<fs_bapix> = 'X'.
ENDIF.
ENDIF.
ENDLOOP.
2013 Oct 25 12:42 PM
Thanks for your answer Max!
I build on top of your solution a FORM which receives my structure and x- structure as variable.
Furthermore, i checked the type and the length of the x-structure field so that only if the field is CHAR and Length 1,
the x-field will be updated. The background is, as Smruti said, that only the fields with datatype BAPIUPDATE should be updated.
Thanks a lot guys!
Here is my solution:
FORM fill_x_structure USING p_struct_name
CHANGING p_structx_name.
DATA: lv_type TYPE c LENGTH 1,
lv_length TYPE integer.
FIELD-SYMBOLS: <fs_bapi> TYPE ANY, "tables as import- parameter
<fs_bapix> TYPE ANY,
<fs_struct_name> TYPE ANY, "fields
<fs_structx_name> TYPE ANY.
ASSIGN p_struct_name TO <fs_struct_name>.
ASSIGN p_structx_name TO <fs_structx_name>.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_struct_name> TO <fs_bapi>.
IF sy-subrc <> 0. EXIT. ENDIF.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_structx_name> TO <fs_bapix>.
* check for type and length of field in x- structure
DESCRIBE FIELD <fs_bapix> TYPE lv_type.
DESCRIBE FIELD <fs_bapix> LENGTH lv_length IN CHARACTER MODE.
IF <fs_bapi> IS NOT INITIAL AND lv_type = 'C' AND lv_length = 1.
<fs_bapix> = 'X'.
ENDIF.
ENDDO.
ENDFORM. " FILL_X_STRUCTURE
2019 Sep 16 5:17 AM
Use following Code:
PERFORM fill_x_structure CHANGING ls_shippingx.
FORM fill_x_structure CHANGING cs_x_structure TYPE any.
DATA: lr_struct TYPE REF TO cl_abap_structdescr,
lr_field TYPE REF TO cl_abap_elemdescr.
FIELD-SYMBOLS: <comp> LIKE LINE OF cl_abap_structdescr=>components,
<x> TYPE ANY.
lr_struct ?= cl_abap_typedescr=>describe_by_data( cs_x_structure ).
CHECK lr_struct IS BOUND.
LOOP AT lr_struct->components ASSIGNING <comp>.
UNASSIGN <x>.
* take only x fields into consideration, if unicode is active C1 takes
2 bytes
CHECK <comp>-type_kind EQ lr_struct->typekind_char.
ASSIGN COMPONENT <comp>-name OF STRUCTURE cs_x_structure TO <x>.
CHECK <x> IS ASSIGNED.
* get element description
lr_field ?= cl_abap_typedescr=>describe_by_data( <x> ).
CHECK lr_field->absolute_name EQ '\TYPE=BAPIUPDATE'.
<x> = cl_mmpur_constants=>yes.
ENDLOOP.
ENDFORM. " fill_x_structure
2019 Sep 16 5:18 AM
PERFORM fill_x_structure CHANGING ls_shippingx.
FORM fill_x_structure CHANGING cs_x_structure TYPE any.
DATA: lr_struct TYPE REF TO cl_abap_structdescr,
lr_field TYPE REF TO cl_abap_elemdescr.
FIELD-SYMBOLS: <comp> LIKE LINE OF cl_abap_structdescr=>components,
<x> TYPE ANY.
lr_struct ?= cl_abap_typedescr=>describe_by_data( cs_x_structure ).
CHECK lr_struct IS BOUND.
LOOP AT lr_struct->components ASSIGNING <comp>.
UNASSIGN <x>.
* take only x fields into consideration, if unicode is active C1 takes
2 bytes
CHECK <comp>-type_kind EQ lr_struct->typekind_char.
ASSIGN COMPONENT <comp>-name OF STRUCTURE cs_x_structure TO <x>.
CHECK <x> IS ASSIGNED.
* get element description
lr_field ?= cl_abap_typedescr=>describe_by_data( <x> ).
CHECK lr_field->absolute_name EQ '\TYPE=BAPIUPDATE'.
<x> = cl_mmpur_constants=>yes.
ENDLOOP.
ENDFORM. " fill_x_structure