Loop Through Source Fields to Populate Target Fields

Jimbo's picture

loop goatWhen creating particularly-complex LSMW objects with dozens of fields in a source file, it can become very repetitive to validate every field if the requirement is to only populate fields with values when the field is populated with a value. This simple snippet of code loops through the fields in the source structure and moves the values in those fields to fields in the target structure where the field of the name is identical.

Start by declaring the internal tables and structures to hold the names of the fields in the source and target structures. This code will be in the GLOBAL_DATA section of the mapping rules.

DATA: typ(1) TYPE c.
FIELD-SYMBOLS: <s>, <g>, <sn>, <gn>.
DATA : l_struc TYPE REF TO cl_abap_structdescr.
DATA : l_typedesc TYPE REF TO cl_abap_typedescr.
DATA : lt_comps TYPE abap_compdescr_tab,
       lt_compg TYPE abap_compdescr_tab,
       w_comps LIKE LINE OF lt_comps,
       w_compg LIKE LINE OF lt_compg.
data: nCounts type i, nCountg type i, cChanges(250) type c.

Next, populate the internal tables with the structures of the source data and the target rule. This code relies on the DESCRIBE_BY_DATA function.

*** Populate an internal table with the names of source fields.
CALL METHOD cl_abap_structdescr=>describe_by_data
  EXPORTING
    p_data      = XD02S
  RECEIVING
    p_descr_ref = l_typedesc.
l_struc ?= l_typedesc.
lt_comps = l_struc->components.
*** Populate an internal table with the names of target fields.
CALL METHOD cl_abap_structdescr=>describe_by_data
  EXPORTING
    p_data      = XD02G
  RECEIVING
    p_descr_ref = l_typedesc.
l_struc ?= l_typedesc.
lt_compg = l_struc->components.

Finally, perform a little validation and then step through the fields of the source structure. Where the source field is non-blank, the code loops through the internal table for the source to find the name of the field, then loops through the internal table for the target in order to determine which field to populate with the value from the field in the source structure.

if XD02S-KUNNR co '1234567890 '.
  shift XD02S-KUNNR right deleting trailing space.
  overlay XD02S-KUNNR with '0000000000'.
endif.
select single KUNNR from KNA1 into XD02G-KUNNR
 where KUNNR eq XD02S-KUNNR.
if sy-subrc ne 0.
  write: / XD02S-KUNNR color col_negative, 'Does not exist.'.
  skip_transaction.
else.
  cChanges = XD02S-KUNNR.
  do.
    assign component sy-index of structure XD02S to <s>.
    if sy-subrc ne 0.
      exit. " Reached the end.
    endif.
    DESCRIBE FIELD <s> TYPE typ.
    if typ eq 'C'.
      nCounts = 0.
      if <s> co ' ' or <s> is initial.
        "Do nothing.
      else.
        loop at lt_comps into w_comps.
          add 1 to nCounts.
          if nCounts eq sy-index and w_comps-NAME ne 'KUNNR'.
            nCountg = 0.
            loop at lt_compg into w_compg.
              add 1 to nCountg.
              if w_compg-NAME eq w_comps-NAME.
                assign component nCountg of structure XD02G to <g>.
                move <s> to <g>.
                concatenate cChanges ',' w_compg-Name ':' <s>
                 into cChanges separated by space.
              endif.
            endloop.
          endif.
        endloop.
      endif.
    endif.
  enddo.
  write: / cChanges.
endif.
XD02G-D0110 = 'X'.
XD02G-D0120 = 'X'.
XD02G-USE_ZAV = 'X'.

This example is a tool designed for a client to allow them to populate an Excel template with fields to be updated and then copy-paste that data into a text file. Afterward, the text file is read into the LSMW object, converted and then run as a batch.

After creating the recording to touch the 54 fields on the extended Address tab in the General Data view, the remainder was to Ctrl-Y, copy, paste the field names into the Excel Template file. Copy and then Paste Special in Excel allows for the data to be transposed from horizontal to vertical at the top of the spreadsheet for the purpose of column headers.

Special thanks to this anonymous programmer for the example of how to coax the field names out of a structure using describe_by_data.

Programming Language: 
ABAP