ABAP 7.50 – CORRESPONDING

But as you all know there is a prominent exception to that rule: All the syntax forms involving CORRESPONDING for assigning structure components that (by chance) have the same name.
  • Before ABAP 7.40, these were mainly MOVE-CORRESPONDING for the components of structures, the CORRESPONDING addition to Open SQL’s SELECT, and some obsolete calculation statements.
  • With ABAP 7.40 MOVE-CORRESPONDING was enabled to handle structured internal tables and a new constructor operator CORRESPONDING was introduced that allows an explicit mapping of structure components with different names.
What was still missing?  A dynamic mapping capability! And this was introduced with ABAP 7.50.
The new system class CL_ABAP_CORRESPONDING allows you to assign components of structures or internal tables with dynamically specified mapping rules.
The mapping rules are created in a mapping table that is passed to a mapping object, e.g. as follows:
DATA(mapper) =
  cl_abap_corresponding=>create(
    source      = struct1
    destination = struct2
    mapping     = VALUE cl_abap_corresponding=>mapping_table(
     ( level   = 0
       kind    = cl_abap_corresponding=>mapping_component
       srcname = ‘…’
       dstname = ‘…’ )
     ( level   = 0
       kind    = cl_abap_corresponding=>mapping_component
       srcname = ‘…’
       dstname = ‘…’ )
     ( level   = 0
       kind    = cl_abap_corresponding=>mapping_component
       srcname = ‘…’
       dstname = ‘…’ ) ) ).
This is a simple example, where all structure components are on top level (0) and where all components are to be mapped (kind = cl_abap_coresponding=>mapping_component). More complicated forms involve nested structures and exclusions. With srcname and dstname the component names  can be specified dynamically. The table setup is similar to the mapping-clause of the CORRESPONDING operator.
After creating the mapping object, all you have to do is to execute the assignment as follows:
mapper->execute( EXPORTING source      = struct1
                 CHANGING  destination = struct2 ).
You can do that again and again for all structures or internal tables that have the same types as those used for creating the mapping object.

Comments

Popular posts from this blog

ABAP 7.40 – Inline Declarations

A Walk through on New and Old Abap Syntax