Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assigning Structure Components →  CL_ABAP_CORRESPONDING - System Class 

CL_ABAP_CORRESPONDING - Simple Assignment

The methods CREATE and EXECUTE of the system class CL_ABAP_CORRESPONDING can be used to assigned components between structure or between internal tables with a dynamically specified mapping rule.

The factory method CREATE is used to create a mapping object:

DATA(mapper) = cl_abap_corresponding=>create( source                = struct|itab
                                              destination           = struct|itab
                                              mapping               = mapping_tab
                                              discarding_duplicates = flag ).

Structures struct or internal tables itab of the assigned data types must be passed to the parameters source and destination. An internal table of the type CL_ABAP_CORRESPONDING=>MAPPING_TABLE, containing the mapping rule, must be passed to the parameter mapping. If an initial mapping table is passed, only the identically named components are assigned. The mapping table has the following components:

  • LEVEL
Level of the components in the structure or row structure. The value 0 stands for the top level.
  • KIND
Mapping type. The possible values are:
  • CL_ABAP_CORRESPONDING=>MAPPING_COMPONENT (1) (The components specified in this row are mapped to each other.)
  • CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_COMPONENT (2) (The component of the source structure specified in this row is excluded from the mapping of identically named components.)
  • CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_ALL (3) (All components of the current source structure are excluded from the mapping of identically name components.)
  • CL_ABAP_CORRESPONDING=>MAPPING_DISCARDING_DUPLICATES (9) (In a source table, duplicate rows are ignored as when using DISCARDING DUPLICATES in a mapping rule of the component operator. The target table must have a unique table key.)
  • SRCNAME
Component of the source structure.
  • DSTNAME
Component of the target (destination) structure.

The rows of the internal table must be constructed so that they produce a mapping rule in the correct order. Components of the source structure for which no mapping is defined and that were not excluded are assigned to identically named components of the target structure.

The method EXECUTE of a mapping object can be used to perform any number of assignments between structures or internal tables src and dst whose data type matches the source type or target type specified when the object was created:

mapper->execute( EXPORTING source      = src
                 CHANGING  destination = dst ).

The assignment is performed component by component

  • between the components specified in the mapping rule
  • between the remaining identically named components at the same level (if not excluded in the mapping rule).

In assignments between structures, components of the target structure to which no components of the source structure are assigned keep their previous value, like the statement MOVE-CORRESPONDING and like the operator CORRESPONDING with the addition BASE. Any nested internal tables are always resolved, as when the addition EXPANDING NESTED TABLES is specified in MOVE-CORRESPONDING or the addition DEEP for the operator CORRESPONDING. In assignments between internal tables, the target table is always initialized first. There is no matching addition for the addition KEEPING TARGET LINES in MOVE-CORRESPONDING or BASE in CORRESPONDING.

If the value "X" was passed to the parameter DISCARDING_DUPLICATES of the method CREATE, duplicate rows are handled in tabular component assignments in the same way as when using the addition DISCARDING DUPLICATES in the basic form of the component operator. Here, the target table must have a unique table key.

The source and the target may be the same. It should be noted, however, the target object is used directly (like in the statement MOVE-CORRESPONDING) and that no temporary intermediate result is created (unlike with the operator CORRESPONDING).

Incorrect parameters passed to the methods of the class CL_ABAP_CORRESPONDING raised exceptions of the class CX_CORR_DYN_ERROR.

Other versions: 7.31 | 7.40 | 7.54


Notes

  • The methods CREATE and EXECUTE of the system class CL_ABAP_CORRESPONDING produce an assignment similar to the statement
dst = CORRESPONDING  #( BASE ( dst ) struct|itab MAPPING ... EXCEPT ... ).
Here, the mapping rule is specified dynamically, however, as the content of a special internal table.

  • The same restrictions apply as in the operator CORRESPONDING. Components can only be mapped to each other if they are on the same level. Components in a substructure cannot be assigned to the components at higher levels, nor higher level components to components in a substructure.

  • The class CL_ABAP_CORRESPONDING always resolves tabular components, which is the same behavior as the operator CORRESPONDING when a mapping rule is specified. In this case, the addition DEEP is also set implicitly.

  • To achieve the same results for standalone in assignments between structures as in the operator CORRESPONDING without BASE, an initial structure can be assigned to the parameter destination.

  • In reflexive assignments between components of the same object, it should be noted that (like in MOVE-CORRESPONDING) the editing order is not defined and that a call of the method EXECUTE cannot be used to switch the content of two components. See the executable example Reflexive Component Assignments.

Example

Uses the class CL_ABAP_CORRESPONDING for assignments of components to a simple structure. The mapping rule dictates that the components a3 are assigned to b1 and a1 to b3. The component a2 is ignored since there are no identically named components in the target structure and b2 keeps its value. a4 and a5 in the target structure also keep their values, however, even though the source structure contains identically named components. This is because the value of CL_ABAP_CORRESPONDING=>MAPPING_EXCEPT_ALL is specified for the mapping type for all non-specified components. The executable example for simple structures enables interactive input of the component names that are mapped to each other.

DATA: 
  BEGIN OF struct1, 
    a1 TYPE string VALUE 'a1', 
    a2 TYPE string VALUE 'a2', 
    a3 TYPE string VALUE 'a3', 
    a4 TYPE string VALUE 'a4', 
    a5 TYPE string VALUE 'a5', 
  END OF struct1, 
  BEGIN OF struct2, 
    b1 TYPE string VALUE 'b1', 
    b2 TYPE string VALUE 'b2', 
    b3 TYPE string VALUE 'b3', 
    a4 TYPE string VALUE 'b4', 
    a5 TYPE string VALUE 'b5', 
  END OF struct2. 

DATA(mapper) = 
  cl_abap_corresponding=>create( 
    source            = struct1 
    destination       = struct2 
    mapping          = VALUE cl_abap_corresponding=>mapping_table( 
      ( level = 0 kind = 1 srcname = 'a1' dstname = 'b3' ) 
      ( level = 0 kind = 1 srcname = 'a3' dstname = 'b1' ) 
      ( level = 0 kind = 3 ) ) ). 

mapper->execute( EXPORTING source      = struct1 
                 CHANGING  destination = struct2 ). 

cl_demo_output=>display( struct2 ).

Executable Examples