SAP Tutorials Blog


 


"Data type and object to work with in the example

TYPES: BEGIN OF st_type,

            comp1 TYPE c LENGTH 5,

            comp2 TYPE i,

            comp3 TYPE i,

        END OF st_type.

DATA it TYPE TABLE OF st_type WITH EMPTY KEY.

it = VALUE #( ( comp1 = 'a' comp2 = 1 comp3 = 30 )

              ( comp1 = 'bb' comp2 = 2 comp3 = 10 )

              ( comp1 = 'ccc' comp2 = 3 comp3 = 20 ) ).


"Constructing a data object with elementary data type using the CONV operator

"One or more helper variables are possible specified after LET

DATA(hi) = CONV string(

        LET name = cl_abap_context_info=>get_user_technical_name( )

            date = cl_abap_context_info=>get_system_date( )

        IN |Hi { name }. Today's date is { date DATE = ISO }.| ).


"Construction similar to the previous example

"Depending on the time, a string is created. In the example, a LET expression

"is specified for each constructor expression.

DATA(time_of_day) = CONV string(

        LET time = cl_abap_context_info=>get_system_time( ) IN

        COND string( LET good = `Good` ending = `ing` IN

                    WHEN time BETWEEN '050001' AND '120000' THEN good && ` morn` && ending  "Good morning

                    WHEN time BETWEEN '120001' AND '180000' THEN good && ` afternoon`

                    WHEN time BETWEEN '180001' AND '220000' THEN good && ` even` && ending

                    ELSE good && ` night`  ) ).



"Getting a particular column name of an existing internal table using RTTI

"An internal table (it contains information on the table's structured type; the

"component names, among others) is assigned to a data object that is declared

"inline. This is an example of making code more concise with constructor expressions 

"and inline declarations. Assume you use extra declarations for the data objects, or 

"use the older ?= operator for the casts. Many more lines of code are required.

DATA(components) = CAST cl_abap_structdescr( CAST cl_abap_tabledescr(

    cl_abap_typedescr=>describe_by_data( it ) )->get_table_line_type( ) )->components.

DATA(comp2_a) = components[ 2 ]-name. "COMP2


"Achieving the result from above even in one statement using LET

DATA(comp2_b) = CONV abap_compname(

    LET comps = CAST cl_abap_structdescr( CAST cl_abap_tabledescr(

                 cl_abap_typedescr=>describe_by_data( it ) )->get_table_line_type( ) )->components

    IN comps[ 2 ]-name ).


"Constructing a structure using local variables

"The example uses the NEW operator to create an anonymous data object

DATA(new_struc) = NEW st_type( LET num = 2 ch = 'AP' IN

                                comp1 = 'AB' && ch comp2 = 2 * num comp3 = 3 * num ).

"Structure content:

"COMP1    COMP2    COMP3

"ABAP     4        6


"Constructing an internal table using local variables

"The example uses the VALUE operator.

"Note the parentheses ( ... ) representing table lines.

DATA(itab_value) = VALUE string_table( LET line = 1 IN

                                        ( |Line { line }| )

                                        ( |Line { line + 1 }| )

                                        ( |Line { line + 2 }| ) ).

"Table line content:

"Line 1

"Line 2

"Line 3


"Using a local field symbol in LET expressions

"- The right-hand side value must be the result of a writable expression, i.e.

"  an operand that can be written to

"- This value is then assigned to the local field symbol (as if ASSIGN is used)

"- In the examples above, a specification such as ... LET <a> = 1 IN ... is not

"  possible as they are not writable expressions.

"- Writable expressions:

"  - Constructor expressions NEW class( ... )->attr and CAST type( ... )->dobj

"  - Table expressions itab[ ... ] and their chainings, e.g. itab[ 1 ]-comp

"In the following example, an internal table is looped over. A string is created

"from the table line content. In the constructor expression, a LET expression is

"specified that uses a field symbol. It is assigned the line of the internal table.

"The sy-index value represents the table index value.

DATA str_tab TYPE string_table.

DO lines( it ) TIMES.

    DATA(concatenated_tab) = CONV string(

        LET <li>  = it[ sy-index ]

            comma =   `, `

        IN  |{ <li>-comp1 }{ comma }{ <li>-comp2 }{ comma }{ <li>-comp3 }| ).

    str_tab = VALUE #( BASE str_tab ( concatenated_tab ) ).

ENDDO.

"Table line content:

"a, 1, 30

"bb, 2, 10

"ccc, 3, 20


Serkan AKKAVAK

Computer Engineer BSc

SAP Department Manager

Contact : serkurumsal@yandex.com