Let’s discuss the conditional operator in ABAP 7.4 new syntax and its uses. The syntax would be like below

result = COND <type>( WHEN <condition/expression> THEN value1 ...
                      ELSE valueX ).

If you put the type as # then it will take the result type as the operand type (in the above syntax value1)

e.g:

REPORT ztest_conditional.

SELECT SINGLE netwr FROM vbak INTO @DATA(lv_netwr) 
                    WHERE vbeln = '0000000010'.

DATA(lv_priority) = COND #( WHEN lv_netwr > 7000 THEN 'High Priority'
                            ELSE 'Less Priority' ).
WRITE: lv_priority.

here in this example, the report will give you the result of the conditional operator. If the netwr is greater than 7000 then variable lv_priority will hold ‘High Priority’ else ‘Less Priority’.


As mentioned above – the result variable lv_priority will hold the first operand data type
Let’s check in debugging to validate it 

Conditional Operator in ABAP 7.4

If you are considering the use of type as # then be cautious about using the same. let’s take an example. here lv_netwr is 7000 

REPORT ztest_conditional.

SELECT SINGLE netwr FROM vbak INTO @DATA(lv_netwr) 
                    WHERE vbeln = '0000000010'.

DATA(lv_priority) = COND #( WHEN lv_netwr > 7000
                            THEN 'High Priority'
                            WHEN lv_netwr > 5000 AND lv_netwr <= 7000
                            THEN 'Medium Priority'
                            ELSE 'Less Priority' ).

WRITE: lv_priority.

Result:

Result of cond operator with use of #

To get the correct result in the above case use the non generic type instead of #

SELECT SINGLE netwr FROM vbak INTO @DATA(lv_netwr) 
                    WHERE vbeln = '0000000010'.

DATA(lv_priority) = COND String( WHEN lv_netwr > 7000
                                 THEN 'High Priority'
                                 WHEN lv_netwr > 5000 AND lv_netwr <= 7000
                                 THEN 'Medium Priority'
                                 ELSE 'Less Priority' ).

WRITE: lv_priority.

To get the correct result you use the type as a non generic type like string

Result:

Result of cond operator with use of String

Note: while using the COND operator if you don’t use the ELSE part, in that case, if none of the conditions are satisfied then it will assign the result variable with the initial value.

Hopefully, this clears the concept of the COND operator.😆

Check out our ABAP Blogs ABAP – ABAP Skill

Check out our OData Series OData – ABAP Skill

Details about COND operator: Conditional Operator in ABAP 7.4


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *