cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Using COND Operator with sy-subrc check

mustafa_74199
Participant
0 Likes
395

Hello, i am facing an interesing issue.

In this piece of code everything works fine.

READ TABLE lt_charge ASSIGNING <ls_charge> WITH KEY key = ls_data-key.
 IF sy-subrc IS INITIAL.
  <ls_output_data>-costs = <ls_output_data>-costs + ( <ls_charge>-amount * 10000 ).
 ENDIF.

But when i change to this version, it always refreshes <ls_output_data>-costs. What is the difference between these 2 versions?

READ TABLE lt_charge ASSIGNING <ls_charge> WITH KEY key = ls_data-key.
 <ls_output_data>-costs = COND #( WHEN sy-subrc IS INITIAL THEN <ls_output_data>-costs + ( <ls_charge>-amount * 10000 ) ).

Accepted Solutions (0)

Answers (1)

Answers (1)

LaurensDeprost
Contributor

Hi Mustafa,

In your second snippet it always refreshes <ls_output_data>-costs because you're missing the ELSE part of the COND statement.

To quote the documentation:

"If none of the logical expressions are true, the result specified after ELSE is selected. If ELSE is not specified, the result is the initial value of the data type."


Try this instead:

<ls_output_data>-costs = COND #(
  WHEN sy-subrc IS INITIAL
    THEN <ls_output_data>-costs + ( <ls_charge>-amount * 10000 )
    ELSE  <ls_output_data>-costs ).
mustafa_74199
Participant
0 Likes

Hi Laurens,

Many thanks for the answer. It works great 🙂

Best Regards from my side.

Sandra_Rossi
Active Contributor

For better legibility, I prefer the original writing 😄

READ TABLE lt_charge ASSIGNING <ls_charge> WITH KEY key = ls_data-key.
IF sy-subrc IS INITIAL.
  <ls_output_data>-costs = <ls_output_data>-costs + ( <ls_charge>-amount * 10000 ).
ENDIF.
LaurensDeprost
Contributor

I agree and prefer IF-ELSE here too.