Do you know about the CL_ABAP_CORRESPONDING system class

0
5214

Dear SAPLearners, in this blog post you will learn about the CL_ABAP_CORRESPONDING system class and its usage.

CL_ABAP_CORRESPONDING

Does the name of the class rings bell of something you already know? Yes, I think you got the answer. CL_ABAP_CORRESPONDING allows to move data between structures or Internal tables through dynamic mappings.

In our earlier blogs we have seen other alternatives to this system class like MOVE-CORRESPONDING, CORRESPONDING but allows only when column names are identical and with static mapping rules.

Also Read: MOVE-CORRESPONDING for internal tables.

There are two main methods in this class CREATE and EXECUTE which allows component assignment between structures or internal tables based on dynamic mapping rules.

CREATE( )

This method is to create the mapping object. Mapping object mainly contains mapping rules, source and destination. The source and destination structures (or) internal tables need not to be of identical column names

EXECUTE( )

This method is to run the data transfer based on the mapping object created using CREATE method.

Lets dive in and learn more about the class CL_ABAP_CORRESPONDING with examples from basic to complex.

Example#1 – Simple Assignment between Structures

Below example will move the data between two simple structures which are of different column names

TYPES:
  BEGIN OF struct1,
    a1 TYPE string,
    a2 TYPE string,
    a3 TYPE string,
  END OF struct1,

  BEGIN OF struct2,
    b1 TYPE string,
    b2 TYPE string,
    b3 TYPE string,
  END OF struct2.

DATA dest_struct TYPE struct2.

DATA(src_struct) = VALUE struct1( a1 = 'Jon Snow'
                                  a2 = 'Arya Stark'
                                  a3 = 'Daenerys').
* Create a Mapping Object
DATA(lo_mapping_object) =
         cl_abap_corresponding=>create(
           source      = src_struct
           destination = dest_struct
           mapping     = VALUE cl_abap_corresponding=>mapping_table(
             ( level = 0 kind = 1 srcname = 'A1' dstname = 'B1' )
             ( level = 0 kind = 1 srcname = 'A2' dstname = 'B2' )
             ( level = 0 kind = 1 srcname = 'A3' dstname = 'B3' )
           ) ).

* Transfer the data from src_struct to dest_struct
lo_mapping_object->execute( EXPORTING source      = src_struct
                            CHANGING  destination = dest_struct ).

cl_demo_output=>display( dest_struct  ).

Example#2 – Simple Assignment between Internal Tables

Below example will move the data between two ABAP Internal tables which are of different column names

TYPES:
  BEGIN OF struc