Skip to Content
Technical Articles

Fill Updated Information Structures in BAPI automatically

Every time you use a BAPI to create a SAP Object there are several structures and tables that need to be filled in with the data requiered to complete the document. Most of the times these structures are acompanied by other structures that contains the flags that tells the BAPI what fields are going to be used or updated. Those structures are always identified by an ‘X’ at the end of the name.

How many times have you spent time and time wondering why a BAPI is not working just to realised that you forgot to fill the corresponding field in the ‘X’ structure? I can tell you that i have spent quite a lot.

For that reason i have developed a method that makes that work for you. You fill a new field in the data structure and this method automatically put the ‘X’ in the corresponding strucutre.

All you need is to create a method in your tools class with the following importing parameters

Import%20Parameters

Importing Parameters

where “DATA_STRUCT” is the structure with the data used in the BAPI and “FLAG_STRUCTURE_NAME” is the name of the ‘X’ structure that acompanies the data structure in the BAPI.

Of course the result of this method will be an exporting structure with the following characteristics

Exporting%20Parameters

Exporting Parameters

 

The code of the method is as follows

cl_reca_ddic_tabl=>get_field_list( EXPORTING id_name = flag_struct_name
                                   IMPORTING et_field_list = DATA(table_data) ).

IF sy-subrc = 0.

   LOOP AT table_data ASSIGNING FIELD-SYMBOL(<fs_field>).

      ASSIGN COMPONENT <fs_field>-fieldname OF STRUCTURE data_struct TO FIELD-SYMBOL(<fs_data>).
      IF sy-subrc <> 0.
         CONTINUE.
      ELSE.
         ASSIGN COMPONENT <fs_field>-fieldname OF STRUCTURE filled_flag_struct TO FIELD-SYMBOL(<fs_datax>).
      ENDIF.

      IF <fs_data> IS NOT INITIAL.
         CASE <fs_field>-rollname.
            WHEN 'BAPIUPDATE'.
              <fs_datax> = abap_true.
            WHEN OTHERS.
              <fs_datax> = <fs_data>.
         ENDCASE.
       ENDIF.

   ENDLOOP.

ENDIF.

There are some fields that are not flags inside the ‘X’ structure so in that case the value of the data structure is replicated in the ‘X’ structure.

With this method you do not have to worry anymore about forgetting to fill the ‘X’ structure correctly.

 

1 Comment
You must be Logged on to comment or reply to a post.
  • Hi!

    Thanks for sharing, this would work just fine for basic cases. I have used similar approaches, but used the more generic cl_abap_structdescr, and not the reca-specific one.

    I would like to mention some edge cases though... This solution will for instance not work when you want to reset a value, like clearing a text or date, or setting some numeric value to 0. Those values will be interpreted as INITIAL and therefore not marked for update.

    Secondly, I see a potential risk that too many fields are marked as X if the implementation is:

    1. Call READ-BAPI
    2. Update some of the fields
    3. Call this method
    4. Call UPDATE-BAPI

    It will "most likely" work just fine in most of the cases and most BAPIs, but I can imagine there are some BAPIs that might for instance log that as changes, or will perform additional checks or whatnot even though the data is not actually changed. (I have no examples, just a general thought to not mark too many 'X')

     

    I don't have sharable code available but another generic approach is to have old_data_struct and new_data_struct as importing parameters, and making an actual comparison between the fields. Passing an "empty" old_data_struct would then make the field value comparison against "INITIAL" so that should work too.