SAP ABAP Loop over internal table1 copying 1 column to a second internal table2 corresponding column on same index

986 views Asked by At

I have a BAPI with importing parameters:

  1. Header line from Purchase order document and
  2. itemdata which is an internal table containing 1 or more Purchase Order (PO) positions. Those PO positions have 5 fields, one of them is short text for items.

Then I have a changing parameter which is a Supplier Invoice (SI) document with also an internal table itemdata but which has only 2 fields. The field "short text" for position line is the same in both PO and Supplier Invoice documents.

Now I want to copy over the short text from PO to SI in case the SI short text is empty.

This should occur for same line index in the PO and SI documents.

I wonder how to do it in ABAP, I first thought MOVE-CORRESPONDING would do it, but it then also copies over all corresponding fields to the other document, but I just want the short text field.

If I am doing MOVE-CORRESPONDING (MAPPING f1 = f1 with EXCEPT *)... then it curiously modifies the other ignored columns by setting default values there... WHY??

Here is an example of that behaviour (Variant5, last line): https://archer4sap.com/sap-abap-7-51_v1/base-corresponding-move-corresponding-operators/

I did this now with ordinary index iteration on the table length and for loops "manually", but I wonder if there is a simpler approach to do it:

REPORT ZZZ_ct.
TYPES: BEGIN OF ti_1,
            field1 type int8,
            field2 type string,
        END OF ti_1,

        BEGIN OF ti_2,
            field1 type int8,
            field2 type string,
        END OF ti_2,


        tti_1 TYPE TABLE OF ti_1 WITH EMPTY KEY,
        tti_2 TYPE TABLE OF ti_2 WITH EMPTY KEY.

*    DATA t1_table type tti_1.
DATA(t1_table) = VALUE tti_1( ( field1 = 1  field2 = 'HALLO 1' )
                     ( field1 = 2 field2 = 'HALLO 2' ) ).

DATA(t2_table) = VALUE tti_2( ( field1 = 3  field2 = 'HOME' )
                     ( field1 = 4 field2 = 'MOUSE' ) ).

DATA(li) = lines( t1_table ).
WRITE: / li.
DATA: cnt type i VALUE 0.

DATA t1_copy_table TYPE TABLE OF ti_1 WITH EMPTY KEY.
*MOVE-CORRESPONDING t1_table TO t1_copy_table.

WHILE cnt < li.

   Write: / 'This is the line:', cnt.
   cnt = cnt + 1.
   READ TABLE t1_table INDEX cnt INTO DATA(t1_wa).
   READ TABLE t2_table INDEX cnt INTO DATA(t2_wa).

   t1_wa-field1 = t2_wa-field1.
   WRITE: / t1_wa-field1, space, t1_wa-field2.
   WRITE: / t2_wa-field1, space, t2_wa-field2.

   APPEND t1_wa TO t1_copy_table.
ENDWHILE.

LOOP AT t1_table into DATA(w1).
  WRITE: / w1-field1, space, w1-field2.
ENDLOOP.


LOOP AT t1_copy_table into DATA(w2).
  WRITE: / 'Copy table: ' , w2-field1, space, w2-field2.
ENDLOOP.

EDIT: Here is the output of the program above:

enter image description here

EDIT: The output should be like in the image below.. I copied only the first field from itable2 into itable1 for the same line index.. is there some inbuilt function to do this?

0

There are 0 answers