VALUE Operator Examples - ABAP 7.4/7.5 New Syntax

VALUE Operator Examples - ABAP 7.4/7.5 New Syntax | ABAP New Syntax With Value Operator

Syntax:

VALUE type( ... ) ...

VALUE - Structures

TYPES: BEGIN OF ty_1,
        name TYPE string,
        role TYPE string,
       END OF ty_1.

1) With Structure and with Work Area inline declaration

DATA(wa1) = VALUE ty_1( name = 'Pradeep' role = 'ABAP').


2) With Structure and with Work Area declared already

DATA: wa1 type ty_1.

wa1 = VALUE #( name = 'Pradeep' role = 'ABAP' ).


3) Using Work Area which is already defined as a BASE for new work area.

The structure of new work area will be determined at run time using base work area wa1.

DATA(wa2) = VALUE #( BASE wa1 name = 'Sachin' role = 'Java' ).


In the above example if the role is not filled it will still take the value from base work area as 'ABAP' but the name will be replaced with 'Sachin'.

DATA(wa2) = VALUE #( BASE wa1 name = 'Sachin' ).


VALUE - Internal Tables

1) With Table type and Internal table inline declaration 

Create a table type tt_st1 with ty_1 structure and with default or empty key.

TYPES: tt_st1 TYPE TABLE OF ty_1 WITH DEFAULT KEY.

DATA(itab1) = VALUE tt_st1( ( name = 'Pradeep' role = 'ABAP')
                            ( name = 'Sachin' role = 'Java')
			    ( name = 'Virat'  role = 'Angular') ).


2) With Internal table declared already.

This is a better approach when compared with first one if you are confused with default key or empty in type declaration.

DATA: itab1 TYPE TABLE OF ty_1

itab1 = VALUE #( ( name = 'Pradeep' role = 'ABAP')
                 ( name = 'Sachin' role = 'Java')
                 ( name = 'Virat'  role = 'Angular') ).


3) Append new entry to internal table using Value.

APPEND VALUE #( name = 'Pranay' role = 'ABAP' ) TO itab1.


4) Using internal table as BASE

itab2 = VALUE #( BASE itab1 ( name = 'Dinesh'  role = 'Python')
                            ( name = 'Rishabh' role = 'Nodejs') ).

ITAB2 will contain records from itab1 and 2 new records added above.

5) Using Value operator to avoid dump while reading internal tables or to provide default value if read fails.


The below statement will throw dump if the entry is not found in internal table.

wa_read = itab1[ name = 'Praveen' ]


To avoid the dump we can use VALUE Operator with Optional extension.

wa_read = VALUE #( itab1[ name = 'Praveen' ] OPTIONAL ).


If the entry is not found we can also pass default values using below code.

wa_def = VALUE #( name = 'ABC' role = 'XYZ' ).
wa_read = VALUE #( itab1[ name = 'Praveen' ] DEFAULT wa_def ).

Post a Comment

Previous Post Next Post