Saturday 4 November 2023

How to add two or more fields in a range table with FOR Expression

Requirement: To form a range table consuming two or more fields from a table in ABAP.

Old or Traditional way of Coding:

*Define the structures of table
TYPES:
  BEGIN OF lty_comp,
    seqno  TYPE char4,
    wtype1 TYPE char4,
    wtype2 TYPE char4,
    wtype3 TYPE char4,
  END OF lty_comp.

*Declare table type
TYPES: gtt_comp      TYPE STANDARD TABLE OF lty_comp WITH DEFAULT KEY,
       lr_lgart_type TYPE RANGE OF lgart.

DATA:  lr_wtype      TYPE lr_lgart_type.

*Populate an Internal Table with some Hardcoded values
DATA(lt_item) = VALUE gtt_comp( ( seqno = '0001' wtype1 = 'WT1A' wtype2 = 'WT2A' wtype3 = 'WT3A' )
                    ( seqno = '0001' wtype1 = 'WT1B' wtype2 = 'WT2B' wtype3 = 'WT3B' )
                    ( seqno = '0001' wtype1 = 'WT1C' wtype2 = 'WT2C' wtype3 = 'WT3C' )
                    ( seqno = '0001' wtype1 = 'WT1D' wtype2 = 'WT2D' wtype3 = 'WT3D' )
                    ( seqno = '0001' wtype1 = 'WT1E' wtype2 = 'WT2E' wtype3 = 'WT3E' )
                    ( seqno = '0001' wtype1 = 'WT1F' wtype2 = 'WT2F' wtype3 = 'WT3F' ) ).

* Range table for wtype 1
lr_wtype = VALUE #( BASE lr_wtype
                    FOR ls_wtype IN lt_item
                    sign = 'I' option = 'EQ' ( low = ls_wtype-wtype1 ) ).
* Range table for wtype 2
lr_wtype = VALUE #( BASE lr_wtype
                    FOR ls_wtype IN lt_item
                    sign = 'I' option = 'EQ' ( low = ls_wtype-wtype2 ) ).
* Range table for wtype 3
lr_wtype = VALUE #( BASE lr_wtype
                    FOR ls_wtype IN lt_item
                    sign = 'I' option = 'EQ' ( low = ls_wtype-wtype3 ) ).

cl_demo_output=>display( lr_wtype ).

Result:

How to add two or more fields in a range table with FOR Expression
Result 1

Another way with few lines of code.

*Define the structures of header & item table
TYPES:
  BEGIN OF lty_comp,
    seqno  TYPE char4,
    wtype1 TYPE char4,
    wtype2 TYPE char4,
    wtype3 TYPE char4,
  END OF lty_comp.

*Declare table type
TYPES: gtt_comp      TYPE STANDARD TABLE OF lty_comp WITH DEFAULT KEY,
       lr_lgart_type TYPE RANGE OF lgart.

DATA:  lr_wtype      TYPE lr_lgart_type.

*Populate an Internal Table with some Hardcoded values
DATA(lt_item) = VALUE gtt_comp( ( seqno = '0001' wtype1 = 'WT1A' wtype2 = 'WT2A' wtype3 = 'WT3A' )
                    ( seqno = '0001' wtype1 = 'WT1B' wtype2 = 'WT2B' wtype3 = 'WT3B' )
                    ( seqno = '0001' wtype1 = 'WT1C' wtype2 = 'WT2C' wtype3 = 'WT3C' )
                    ( seqno = '0001' wtype1 = 'WT1D' wtype2 = 'WT2D' wtype3 = 'WT3D' )
                    ( seqno = '0001' wtype1 = 'WT1E' wtype2 = 'WT2E' wtype3 = 'WT3E' )
                    ( seqno = '0001' wtype1 = 'WT1F' wtype2 = 'WT2F' wtype3 = 'WT3F' ) ).

* Range table for wtype 1/2/3
lr_wtype = VALUE #( BASE lr_wtype
                    FOR ls_wtype IN lt_item
                    sign = 'I' option = 'EQ' ( low = ls_wtype-wtype1 )
                               option = 'EQ' ( low = ls_wtype-wtype2 )
                               option = 'EQ' ( low = ls_wtype-wtype3 ) ).

cl_demo_output=>display( lr_wtype ).

Result 2

How to add two or more fields in a range table with FOR Expression
Result 2

Only difference between two results is the low value gets reordered due to for expression.

No comments:

Post a Comment