So recently in a program, I am trying to clear couple of columns in an internal table. So instead of doing it the regular old way of looping into reference and clearing those fields, I used the new ABAP to do that job. Don’t reason with me on this, I am addicted to the new ABAP 😛 .

The first issue I faced is the internal table getting cleared completely(emptied) instead of just clearing just one column. See the code below:

test_table1 = VALUE #( FOR <test_line1> IN test_table1 ( VALUE #( BASE <test_line1> field1 = '' ) ) ).

This exact issue is pointed out in the below blog. So the ‘VALUE’ operator clears the left side before the processing of the right side and so it will return empty table instead of clearing it. See that blog in case if you’ve missed it. It is very interesting.

https://blogs.sap.com/2019/05/27/abap-traps-dont-self-reference-in-constructor-operators/

Now for this, I did the same. I used the “LET” expression to store my internal table data in a temporary table and cleared the column. Now test_table1 data will be cleared after assigning the test_table1 data to temp_table and we will do the for loop on temp_table.

test_table1 = VALUE #(  LET temp_table = test_table1 IN FOR <test_line1> IN temp_table ( VALUE #( BASE <test_line1> field1 = '' ) ) ).

 

It worked 🙂 and it was all fun and games until my boy ABAP compiler started to throw errors 😀 .

The thing is I assumed that the LET expression in ABAP behaves same as the LET statement in Javascript.

JavasSript “let”:

The difference is in the Javascript, LET variables are block scoped, if the control comes out of the block(if or for loop etc..,), the LET variable will lose it’s scope and becomes undefined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

ABAP “LET”

But in ABAP, LET variables behaves similar to a normal variable declared using DATA except that you cannot access it outside the constructor expressions but they can be reused inside other constructor expression but we should pass the same type to the LET variable and it will become initial.

This is the reason for my errors as I’ve used the same LET variable to clear multiple tables.

There is an another way given by SAP where we can use the field symbol in the LET expression  and you might think that it will hold the reference of the assigned variable and will be again be assigned with a different type in an another constructor expression( VALUE #( ) etc., ) but guess what, even the field symbol type is also fixed once used in the LET expression.

Note: It will not work the same way as variable ( LET <fs> = table will not work), I tried it with a different approach and found that it still fixes the type of <fs> and will not be updated in an another LET expression.

Obviously I shouldn’t compare two different languages as they have their limitations. But it would be nice if it works the same like a JavaScript “Let” variable at least the field symbol part of it. (in the future)

 

ABAP Documentation Link:

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abaplet.htm

 

UPDATE:

If you simply want to clear a column without conditions you can go for modify or corresponding using except. For conditional based you can go with this approach. see the comments below by Alexander K and Dmitriy Kurshakov

https://blogs.sap.com/2019/07/15/dont-let-it-fool-yall-javascript-folks.-abap-let-vs-javascript-let./#comment-466188

 

BTW keep the let expression name simple. See the comments below bySuhas Saha

https://blogs.sap.com/2019/07/15/dont-let-it-fool-yall-javascript-folks.-abap-let-vs-javascript-let./#comment-466230

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x