Application Development 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 fill dynamic fields of a Structure with a Condition?

0 Kudos

Hello,

im using the Bapi ' BAPI_PO_CREATE1' and i would like to fill the fields of the update structure or tables dynamic.

My code:

*     fill header structure

         ls_po_header-po_number = '9000000020'.

         ls_po_header-comp_code = '1000'.

         ls_po_header-doc_type = 'ZFIT'.

         ls_po_header-item_intvl = '00001'.

         ls_po_header-vendor = '1000'. "000000001000

         ls_po_header-pmnttrms = '0001' .

         ls_po_header-purch_org = '1000' .

         ls_po_header-pur_group = '001' .

         ls_po_header-currency = 'EUR'.

*      Headerx Struktur füllen (Update Struktur)

         ls_po_headerx-po_number = 'X'.

         ls_po_headerx-comp_code = 'X'.

         ls_po_headerx-doc_type = 'X'.

         ls_po_headerx-item_intvl = 'X'.

         ls_po_headerx-vendor = 'X'.

         ls_po_headerx-pmnttrms = 'X'.

         ls_po_headerx-purch_org = 'X'.

         ls_po_headerx-pur_group = 'X'.


My goal:


To fill the fields of the structure ls_po_headerx  with 'X' if in the corresponding fields of the structure  ls_po_header is not inital.


Same mission i got on the internal table lt_po_items and lt_po_itemsx.


Good Luck!

1 ACCEPTED SOLUTION

gabmarian
Active Contributor
0 Kudos

Given that the orders of fields are the same in both structure you can do something like that:

DO.

  

     ASSIGN COMPONENT SY-INDEX OF 'LS_PO_HEADER' TO STRUCTURE <FIELD>.

     IF <FIELD> IS NOT ASSIGNED.

          EXIT.

     ENDIF.

  

     ASSIGN COMPONENT SY-INDEX OF 'LS_PO_HEADERX' TO STRUCTURE <FIELD_X>.

     IF <FIELD> IS NOT INITIAL.

          <FIELD_X> = ABAP_TRUE.

     ENDIF.

     UNASSIGN <FIELD>

ENDDO.

9 REPLIES 9

former_member195431
Participant
0 Kudos

Hello Julian,

Can you put conditions like: -

IF ls_po_header-po_number IS NOT INITIAL.

     ls_po_headerx-po_number = 'X'.

ENDIF.

Just a thought.

Thanks

Rajit

sabirshah1
Participant
0 Kudos

Hi,

You can use Case statement of if else statment to fill the values on certain condition.

Like

IF ls_po_header-po_number IS NOT INITIAL.


     ls_po_headerx-po_number = 'X'.


ENDIF.

Best Regards

Sabir

Former Member
0 Kudos

hi Julian,

I don't have ready-to-use code for that, but thats possible as the fieldnames of both structures are identical.

You should retrieve the field catalog of the structure ls_po_header using RTTS (cl_abap_struct*...). Then loop at this table of fields and do a "ASSIGN component [fieldname] of structure ls_po_header to <lv>" (where <lv> is a field-symbol of type any). Then you check: is this field not initial? If yes, do an ASSIGN component again but of the structure   ls_po_headerX and set the field to abap_true (X).

Roland

former_member182550
Active Contributor
0 Kudos

Hi,

I tend to use a macro for this,  something along the lines of:


Define Fill_Po.
       ls_po_Header-&1 = &2.
       ls_po_Headerx-&1 = Abap_True.
End-Of-Definition

And then when you want to populate your structure:


Fill_Po: Po_Number '9000000020',
         Comp_Code '1000',
         Doc_Type  'ZFIT'.

Regards,

Rich

gabmarian
Active Contributor
0 Kudos

Given that the orders of fields are the same in both structure you can do something like that:

DO.

  

     ASSIGN COMPONENT SY-INDEX OF 'LS_PO_HEADER' TO STRUCTURE <FIELD>.

     IF <FIELD> IS NOT ASSIGNED.

          EXIT.

     ENDIF.

  

     ASSIGN COMPONENT SY-INDEX OF 'LS_PO_HEADERX' TO STRUCTURE <FIELD_X>.

     IF <FIELD> IS NOT INITIAL.

          <FIELD_X> = ABAP_TRUE.

     ENDIF.

     UNASSIGN <FIELD>

ENDDO.

0 Kudos

Thank you Gábor!      

That was not 100% correct, i solved it like this:

DO.

*     ASSIGN COMPONENT SY-INDEX OF 'LS_PO_HEADER' TO STRUCTURE <FIELD>.

   ASSIGN COMPONENT sy-index OF STRUCTURE ls_po_header TO <field>.

   IF <field> IS NOT ASSIGNED.

     EXIT.

   ENDIF.

*     ASSIGN COMPONENT SY-INDEX OF 'LS_PO_HEADERX' TO STRUCTURE <FIELD_X>.

   ASSIGN COMPONENT sy-index OF STRUCTURE ls_po_headerx TO <field_x>.

   IF <field> IS NOT INITIAL.

     <field_x> = abap_true.

   ENDIF.

   UNASSIGN <field>.

ENDDO.



raymond_giuseppi
Active Contributor
0 Kudos

Use DDIF_NAMETAB_GET to get a list of fields, then loop on the returned table and assign each field to a field-symbol if both successful, set or clear the field in the X structure. Also insure the X-field is actually a BAPIUPDATE field, some X structures begin with some key values. (e.g. BAPIMEPOITEMX-PO_ITEM)

Sample:


CALL FUNCTION 'DDIF_NAMETAB_GET'

  EXPORTING

    tabname  = 'BAPIMEPOHEADERX'

  TABLES

    dfies_tab = dfies_tab

  EXCEPTIONS

    not_found = 1

    OTHERS   = 2.

IF sy-subrc <> 0.

  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

LOOP AT dfies_tab ASSIGNING <dfies> WHERE rollname = 'BAPIUPDATE'.

  ASSIGN COMPONENT <dfies>-fieldname OF STRUCTURE wa_poheaderx TO <headerfieldx>.

  CHECK <headerfieldx> IS ASSIGNED. " not actually required?

  ASSIGN COMPONENT <dfies>-fieldname OF STRUCTURE wa_poheader TO <headerfield>.

  CHECK <headerfield> IS ASSIGNED.

  IF <headerfield> IS INITIAL.

    CLEAR <headerfieldx>.

  ELSE.

    <headerfieldx> = abap_true.

  ENDIF.

ENDLOOP.

Hint: You could also replace the DDIF module with some RTTS class like cl_abap_structdescr.

Regards,

Raymond

0 Kudos

Thank you Raymond for those ideas

0 Kudos

There is an argument to say that since you are only filling two structures and you know what those structures are you shouldn't need the sledgehammer above.  Having said that though,  if you have a program where the structures may vary at run time (you're doing the same thing across many different bapi's for example),  then the above is a good way of doing it and should always be kept in your bag of tools.

However,  as this is for one bapi only with two structures then (as I have been told more than a few times),  abide by the KISS principal.

However,  if you are doing a full load for this bapi including header data,  foreign data,  items, shipping etc etc etc then Raymonds is the way to go because it can be used for all of these structures or tables.

Regards,

Rich