Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Avoid IF-ELSE/CASE for below code with new Syntax

sh4il3sh
Participant
0 Likes
2,558

Hi Experts,

I have below code which I would like to write in one go with new syntax avoiding IF-ELSE or Case Statement.
Can you please suggest the same for below?

IF p_vat IS NOT INITIAL.
    gt_final = VALUE #(  FOR ls_vat IN gt_vat_bp
                        ( partner = ls_vat-innuar
                          stceg = ls_vat-stceg
                         )
                      ).
  ELSEIF p_vertex IS NOT INITIAL.
    gt_final = VALUE #( FOR ls_vertex IN gt_vertex_bp
                        ( partner = ls_vertex-innuar
                          country = ls_vertex-country
                          region = ls_vertex-region
                          txjcd = ls_vertex-txjcd
                         )
                      ).
  ELSEIF p_gmap IS NOT INITIAL.
    gt_final = VALUE #( BASE gt_final
                        FOR ls_gmap IN gt_gmap_bp
                        ( partner = ls_gmap-innuar
                          country = ls_gmap-country
                          pstcd1 = ls_gmap-postcode1
                          street = ls_gmap-street
                          housenumber = ls_gmap-housenum
                         )
                      ).
  ENDIF.
*Expecting something like below:

gt_final = do all above processing based on COND/SWITCH maybe?

*This should remove any need to if-else and pick which FOR LOOP to be run based on whats chosen as radiobutton parameter.

Thanks.
3 REPLIES 3
Read only

Sandra_Rossi
Active Contributor
2,494

I guess you know the COND constructor operator, right? What did you try? What is your issue?

Read only

FredericGirod
Active Contributor
2,494
gt_final = cond #( when p_vat is not initial 
then value #( ..... )
Read only

sh4il3sh
Participant
2,494

Thanks Frederic and Sandra.
I was expecting a type to be there in place of hash and I was getting errors.

It is done:

gt_final = COND #(
        WHEN p_map IS NOT INITIAL THEN VALUE #(
                      FOR ls_gmap IN gt_gmap_bp
                        ( partner = ls_gmap-innuar
                          country = ls_gmap-country
                          pstcd1 = ls_gmap-postcode1
                          street = ls_gmap-street
                          housenumber = ls_gmap-housenum
                          city = ls_gmap-city
                          region = ls_gmap-region )
                                                 )"GMAP 
        WHEN p_vat IS NOT INITIAL THEN VALUE #(
                      FOR ls_vat IN gt_vat_bp
                      ( partner = ls_vat-innuar
                        stceg = ls_vat-stceg )
                                                ) "VAT 
        WHEN p_ver IS NOT INITIAL THEN VALUE #(
                      FOR ls_vertex IN gt_vertex_bp
                      ( partner = ls_vertex-innuar
                        country = ls_vertex-country
                        region = ls_vertex-region
                        pstcd1 = ls_vertex-pstcd1
                        city = ls_vertex-city
                        county = ls_vertex-county
                        txjcd = ls_vertex-txjcd )
                                                 ) "Vertex 
                    ). "This is COND Bracket