Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to dynamically fill update/ X structure for BAPI_MATERIAL_SAVEDATA

treee111
Explorer
0 Kudos
6,976

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!

1 ACCEPTED SOLUTION

Former Member
0 Kudos
2,500

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

8 REPLIES 8

nabheetscn
SAP Champion
SAP Champion
0 Kudos
2,500

Simply when you are filling BAPI_MARA then you update the flag also..? Are you doing some move-corresponding stuff../?

0 Kudos
2,500

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!

Former Member
0 Kudos
2,501

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

Former Member
0 Kudos
2,500

Hi ,

As per   it 'Ok'  but some case like BAPI_MARC and BAPI_MARCX , you can check both Structure having Plant which accept Plant Number with Four Character . so my point of check below code where you assign 'X'  to those field only which length 

DFIES_TAB-LENG EQ '000001'.

Regard's

Smruti

0 Kudos
2,500

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.

treee111
Explorer
2,500

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

Former Member
2,500

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

Former Member
0 Kudos
2,500
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