What’s New In ABAP – Series 01, Episode 08 – LET Me Express.

Nagendra Babu
Authored by

Let Expression:

As per SAP help document, a LET expression defines variables var1var2, … or field symbols <fs1><fs2>, … as local helper fields in an expression and assigns values to them. When declared, the auxiliary fields can be used in the operand positions of the expression. There is no way of accessing an auxiliary field statically outside  its expression.

Any clue on whats going on here?. When I read for the first time it was bit confusing and I had to read more than couple of  times. What are local helper fields in this context?.

Let me break it for you, a helper field is nothing but a local variable where you assign a value to them and use it where you want with the helper field name.

E.g.:
DATA lv_name TYPE string 'Ink your Code'. " Here lv_name is the helper field or Helper variable.
cl_demo_output=>write( lv_name).
cl_demo_output=>display( ).

Now we will see how we define helper fields in LET expression with an example given in SAP help document.

TYPES:
BEGIN OF struc,
col1 TYPE i,
col2 TYPE i,
END OF struc.

DATA(rnd) = cl_abap_random_int=>create(
seed = CONV i( sy-uzeit ) min = 1 max = 10 ).

DO 5 TIMES.
DATA(struc) = VALUE struc(


LET x = rnd->get_next( )
y = x * x
z = sy-index * 1000 IN col1 = x + z
col2 = y + z ).

cl_demo_output=>write( struc ).
ENDDO.
cl_demo_output=>display( ).

The highlighted code is the LET expression on the right side we have 3 helper fields x, y, z and it is followed by IN key word.  Whatever the expression defined in after ‘IN’ the helper fields are used. i.e. the values( Col1 and Col2) of ‘struc’ (declared as inline) are assigned based on the LET expression.

go to next episode Fileter it!

 

E.g 2:

Prior to ABAP 7.40.

1.DATA: lv_days_in_year TYPE i VALUE 365,
2.lv_age TYPE i VALUE 26,
3.lv_string TYPE string.

4. lv_days_lived = lv_days_in_year * lv_age.
6.WRITE: lv_string.

Output:

9, 490

With ABAP 7.40

With the help of LET expression I can write it as below.

1. lv_cal TYPE string.
2.DATA(lv_days_lived) = VALUE i( LET lv_days_in_year = 365 lv_age = 26 IN lv_cal = lv_days_in_year * lv_age).
3.WRITE: lv_days_lived.

Output:
9, 490 

In the above example lv_days_in_year and lv_age are help variables used in the expression lv_cal = 365 * 26 and the value is assigned to variable lv_days lived by converting to integer. As specified in the VALUE operator which is ‘i’.

The helper variables can only be used in their own expression, which means that short names (possibly even single-character names) are enough.

End of this section, go to next episode Filet it! or series home page

Got any queries? Please go ahead to the comment section and post your query. Happy to help :).