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 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, tty_struct1 TYPE TABLE OF struct1 WITH EMPTY KEY. DATA dest_itab TYPE STANDARD TABLE OF struct2. DATA(src_itab) = VALUE tty_struct1( ( a1 = 'Jon Snow' a2 = 'Arya Stark' a3 = 'Daenerys' ) ( a1 = 'Tyrion' a2 = 'Sansa Stark' a3 = 'Cersei' ) ). * Create a Mapping Object DATA(lo_mapping_object) = cl_abap_corresponding=>create( source = src_itab destination = dest_itab 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_itab CHANGING destination = dest_itab ). cl_demo_output=>display( dest_itab ).
The output of the above code looks like below
So, we saw few basic use-cases of CL_ABAP_CORRESPONDING, before going any further with some complex examples we will give a pause and learn more about the mapping object.
In the above examples, you have seen the CREATE( ) method in CL_ABAP_CORRESPONDING class to create a mapping object/rules. The mapping table has the following parameters to be passed while creating the mapping object.
LEVEL
Level of the component in the structure or internal table. The possible values for this parameter are from 0 to n, where 0 is top-level.
When you want to move the data between nested structures/internal tables, this parameter is used to define the level of the component.
KIND
defines the Mapping Type. Possible values are
- CL_ABAP_CORRESPONDING=>MAPPING_COMPONENT (or) 1 – The field name specified in srcname and dstname are mapped each other
- CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_COMPONENT (or) 2 – The field name specified in srcname is ignored while mapping if identical field names exists.
- CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_ALL (or) 3 – if source and destination structure/internal table have common field names, all these field names are ignored while mapping.
- CL_ABAP_CORRESPONDING=>MAPPING_DISCARDING_DUPLICATES (or) 9 – duplicate rows in source internal table are ignored while mapping, the destination internal table will have unique rows.
SRCNAME
Specify the field name of source structure or internal table
DSTNAME
Specify the field name of destination structure or internal table
By passing different values for the above parameters you can create the desired mapping object with different mapping rules
Now that we know about CL_ABAP_CORRESPONDING system class and its use-cases, lets continue this in our next blog with complex use-cases.
Also Read: New Constructor operators in ABAP 7.4
Congrats!! you have successfully learned about the ABAP system class CL_ABAP_CORRESPONDING.
Please feel free to comment and let us know your feedback. Subscribe for more updates.
If you liked it, please share it! Thanks!