ABAP New Syntax Lecture 6: CORRESPONDING with MAPPING, EXCEPT, BASE and DEEP

ABAP NEW SYNTAX COURSE
LECTURE 6
CORRESPONDING
MAPPING · EXCEPT · BASE · DEEP

CORRESPONDING builds a target structure or internal table by assigning identically named components and converting compatible values. It is especially useful when modernising MOVE-CORRESPONDING code or translating between API, RAP and legacy types.

Learning outcome: Copy matching fields safely, rename fields with MAPPING, omit fields with EXCEPT, preserve target values with BASE, and understand deep versus shallow mapping.

1. Basic structure conversion

When source and target structures share component names, the basic expression is concise and type-safe.

DATA(ls_api) = CORRESPONDING ty_api_order( ls_db ).

Only corresponding target components are filled. Components that have no source counterpart keep their initial value.

2. Rename fields with MAPPING

CODE SCREEN 1 — MAPPING
DATA(ls_api) = CORRESPONDING ty_api_order(
  ls_db
  MAPPING
    order_id = vbeln
    customer = kunnr
    net_value = netwr ).

In a mapping pair, the target component is written on the left and the source component on the right.

3. Exclude sensitive or technical fields

DATA(ls_public) = CORRESPONDING ty_public_order(
  ls_internal
  EXCEPT created_by changed_at ).

EXCEPT prevents automatic assignment of the listed identically named components. It is useful for audit fields, technical flags or values that must be recalculated.

4. Preserve existing target values with BASE

CODE SCREEN 2 — BASE
ls_target = CORRESPONDING #(
  BASE ( ls_target )
  ls_source
  EXCEPT status ).

BASE starts from an existing target value before applying the source mapping. Without BASE, components not supplied by the source are initial in the new result.

5. Internal table conversion

DATA(lt_api) = CORRESPONDING ty_api_orders(
  lt_db
  MAPPING order_id = vbeln
          customer = kunnr ).

For table operands, mapping is performed line by line. Target key rules still apply. If duplicate target keys are possible, analyse the data first or use DISCARDING DUPLICATES only when silently ignoring duplicates is a deliberate business rule.

6. DEEP and nested components

By default, mapping of nested structured or tabular components is shallow. The DEEP addition enables recursive mapping of suitable nested components; EXPANDING NESTING and KEEPING TARGET LINES are specialised options for nested table scenarios.

DATA(ls_deep_target) = CORRESPONDING ty_deep_target(
  DEEP ls_deep_source ).
Release note: CORRESPONDING additions vary by ABAP release and language version. Use F1 help or the current ABAP Keyword Documentation for your target system before adopting advanced additions.

7. CORRESPONDING or VALUE?

  • Use CORRESPONDING when most fields should be copied by name.
  • Use VALUE when you want to construct the result explicitly.
  • Use MOVE-CORRESPONDING when an in-place statement is clearer than an expression.
  • Prefer explicit MAPPING at system boundaries so future readers can see the contract.

8. Practical exercise

  1. Create source and target order structures with three matching and two differently named fields.
  2. Map the differently named fields using MAPPING.
  3. Keep an existing target status with BASE and EXCEPT.
  4. Convert an internal table and test what happens when a unique target key receives duplicates.

9. Interview quick checks

Which side of a MAPPING pair is the target?

The target component is on the left; the source component is on the right.

What does BASE change?

It starts the new result from an existing target value so components not overwritten by the source can be preserved.

When is DEEP relevant?

When nested structures or table components should be mapped recursively instead of only copying at the top level.

Official SAP references

Educational examples. Validate syntax availability, DDIC types, keys and conversion behavior on your ABAP release.

Post a Comment

0 Comments