Use of CL_ABAP_CORRESPONDING with flat structure

vV4Kim1465653302CL_ABAP_CORRESPONDING class provides simple methods for field mapping between structures.

 

 

 

The post describes how CL_ABAP_CORRESPONDING methods can be used for data transfer between two simple flat structures.

12

CODE
DATABEGIN OF src,
                             col1 TYPE string VALUE ‘AA’,
                            col2 TYPE string VALUE ‘BB’,
                             col3 TYPE string VALUE ‘CC’,
                             col4 TYPE string VALUE ‘DD’,
      END OF src,
      BEGIN OF tgt,
                         fld1 TYPE string VALUE ‘WW’,
                         fld2 TYPE string VALUE ‘XX’,
                        fld3 TYPE string VALUE ‘YY’,
                        fld4 TYPE string VALUE ‘ZZ’,
      END OF tgt.
WRITE:‘SOURCE STRUCTURE’ COLOR 3.
WRITE:/1‘COL1’10‘COL2’30‘COL3’40‘COL4’.
WRITE:/1 srccol110 srccol230 srccol340 srccol4.
WRITE:‘TARGET STRUCTURE’ COLOR 3.
WRITE:/1‘FLD1’10‘FLD2’30‘FLD3’40‘FLD4’.
WRITE:/1 tgtfld110 tgtfld230 tgtfld340 tgtfld4.
TRY.
    DATA(mapper=  cl_abap_corresponding=>create(
                        source      src
                        destination tgt
                        mapping     =  VALUE cl_abap_corresponding=>mapping_table(
                                      level kind srcname ‘COL1’ dstname ‘FLD4’ )
                                      level kind srcname ‘COL2’ dstname ‘FLD2’ )
                                      level kind srcname ‘COL3’ dstname ‘FLD3’ )
                                      level kind srcname ‘COL4’ dstname ‘FLD1’ )
                                      ).
    mapper->executeEXPORTING source      src
                     CHANGING  destination tgt ).
  CATCH cx_corr_dyn_error.
ENDTRY.
SKIP 3.
WRITE:‘AFTER: TARGET STRUCTURE’ COLOR 3.
WRITE:/1‘FLD1’10‘FLD2’30‘FLD3’40‘FLD4’.
WRITE:/1 tgtfld110 tgtfld230 tgtfld340 tgtfld4.


OUTPUT

Mapping is defined as: COL1->FLD4, COL2->FLD2,COL3->FLD3,COL4->FLD1

3


 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s