Skip to Content
Author's profile photo Horst Keller

ABAP News for Release 7.51 – CL_ABAP_CORRESPONDING Completed

With release 7.50 we introduced the system class CL_ABAP_CORRESPONDING that allows you to do dynamically what you can do with the CORRESPONDING operator statically.

There were two gaps in CL_ABAP_CORRESPONDING that are closed now.

 

Usage of a Lookup Table

The CORRESPONDING operator has two variants, one base form and one with a lookup table. The latter constructs an internal table by looking up another table and using the lines found there as sources of the corresponding assignment:

TYPES: 
  BEGIN OF line, 
    value   TYPE i, 
    comment TYPE string, 
  END OF line, 
  itab1 type STANDARD TABLE OF line WITH EMPTY KEY, 
  itab2 TYPE HASHED TABLE OF line WITH UNIQUE KEY value. 

DATA(itab1) = VALUE itab1( for i = 1 UNTIL i >= 10 ( value = i ) ). 
DATA(itab2) = VALUE itab2( ( value = 2 comment = `...` ) 
                           ( value = 3 comment = `...` ) 
                           ( value = 5 comment = `...` ) 
                           ( value = 8 comment = `...` ) ). 

itab1 = CORRESPONDING itab1( itab1 FROM itab2 USING value = value ). 

In this simple example, the lines of  itab1 that are available in itab2 are taken over to itab1.

In release 7.50, this opportunity was missing for CL_ABAP_CORRESPONDING  but is made available in release 7.51:

TYPES: 
  BEGIN OF line, 
    value   TYPE i, 
    comment TYPE string, 
  END OF line, 
  itab1 TYPE STANDARD TABLE OF line WITH EMPTY KEY, 
  itab2 TYPE HASHED TABLE OF line WITH UNIQUE KEY value. 

DATA(itab1) = VALUE itab1( FOR i = 1 UNTIL i >= 10 ( value = i ) ). 
DATA(itab2) = VALUE itab2( ( value = 2 comment = `...` ) 
                           ( value = 3 comment = `...` ) 
                           ( value = 5 comment = `...` ) 
                           ( value = 8 comment = `...` ) ). 

DATA(mapping_tab) = VALUE cl_abap_corresponding=>mapping_table( 
  ( level = 0 kind = 4 srcname = 'PRIMARY_KEY' ) 
  ( level = 0 kind = 5 srcname = 'VALUE' dstname = 'VALUE' ) ). 

cl_abap_corresponding=>create_using( 
     destination       = itab1 
     using             = itab2 
     mapping           = mapping_tab 
     )->execute_using( EXPORTING using       = itab2 
                       CHANGING  destination = itab1 ). 

This example does the same using CL_ABAP_CORRESPONDING as the example for CORRESPONDING above. In real life you use variables instead of literals of course.

For more information see CL_ABAP_CORRESPONDING – Lookup Table.

 

Reflexive Assignments

In release 7.50 source and target must not be the same when using CL_ABAP_CORRESPONDING otherwise the runtime error CORRESPONDING_SELF can occur.

See the respective example for release 7.50,  where the reflexive assignments with CL_ABAP_CORRESPONDING are prepared but not possible yet.

Now see the same example for release 7.51. Reflexive assignments with CL_ABAP_CORRESPONDING are possible now.

    cl_abap_corresponding=>create(
        source      = str1
        destination = str1
        mapping     = VALUE cl_abap_corresponding=>mapping_table(
          ( level = 0 kind = 1 srcname = 'a4' dstname = 'a1' )
          ( level = 0 kind = 1 srcname = 'a3' dstname = 'a2' )
          ( level = 0 kind = 1 srcname = 'a1' dstname = 'a3' )
          ( level = 0 kind = 1 srcname = 'a2' dstname = 'a4' ) )
        )->execute( EXPORTING source      = str1
                    CHANGING  destination = str1 ).

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Tobias Schnur
      Tobias Schnur

      Hi Experts,

      What's the real benefit of using CL_ABAP_CORRESPONDING instead of the CORRESPONDING Operator? That's still not clear to me.

      Tobias

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      "What’s the real benefit of using CL_ABAP_CORRESPONDING instead of the CORRESPONDING Operator?"

      Dymamic v.s. static.

      See an example. The mapping is defined via user input.

      Author's profile photo Emanuel Grabler
      Emanuel Grabler

      Hi Horst,

      first of all thanks for another great block post.

      As I did not find a dedicated posting about the newly introduced addition 'DISCARDING DUPLICATES', I hoped this would be the right place to ask.

      Is there any possibility to make the statement also 'work' for non-unique keys in the target table?

      Thanks and regards,

      Emanuel 

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

      The addition DISCARDING DUPLICATES prevents an exception when duplicate entries occur with respect to a unique key. What do do you want it to do for non-unique keys where there are not such exceptions anyway?

      Author's profile photo Former Member
      Former Member

       

      Hello Horst,

      Great to see that ABAP is still evolving. Some of the new stuff delivered in 7.40+ are really great. ABAP looks a bit more modern now.

      I have a question though regarding the CORRESPONDING operator. Seems that you guys have missed something when implementing the operator functionality. Unless I’m wrong. But let’s consider the following example:

      DATA lt_string TYPE TABLE OF string.
      LOOP AT lt_tse05 ASSIGNING FIELD-SYMBOL(<se05>).
      APPEND INITIAL LINE TO lt_string ASSIGNING FIELD-SYMBOL(<string>).
      <string> = <se05>-line.
      ENDLOOP.

      I would like to shorten it to just one statement:
      lt_string = CORRESPONDING #( lt_tse05 MAPPING table_line = line ).

      However currently I’m geting an error message that type string is not a structure. On one hand side sounds correct, but on the other hand side the good old ABAP let me to refer to a column-less internal table row using the “table_line” keyword. With the new CORRESPONSING operator I’m not able to do this.

      Let me know what you think. Perhaps I’m mising something to write this correctly using the new syntax. If not – then perhaps you guys could fix this in the next NW releases?

      Thanks & regards

      Grzegorz

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

       

      Your observation is correct.

      7.50:

      The pseudo component table_line cannot be specified as a component of an internal table in the mapping rule.

       

      7.52

      It is now possible to specify the pseudo component table_line as a source component of a source table in mapping rules of the component operator CORRESPONDING, if the table has an elementary row type. In all other cases, the behavior is undefined when the pseudo component is specified.

      Unfortunately, still incomplete.

       

       

       

      Author's profile photo Wu Feng
      Wu Feng

      We got a error CX_CORR_DYN_ERROR:The mapper is not created for this target component? any idea?