Application Development 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: 

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

sh4il3sh
Participant
0 Kudos
830

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

Sandra_Rossi
Active Contributor
766

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

FredericGirod
Active Contributor
766
gt_final = cond #( when p_vat is not initial 
then value #( ..... )

sh4il3sh
Participant
766

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