Dynamic coding with multiple values | Community
Skip to main content
Level 3
September 26, 2017

Dynamic coding with multiple values

  • September 26, 2017
  • 3 replies
  • 5836 views

What is the proper way to handle dynamic coding with multiple values?

Should we add an IN statement to syntax?

(  targetData.ita_contact_hist_enrich.xcoh_state_enrich IN ("CT", "VA","MN", "PA","OH") )

OR WOULD IT BE:

<%
                if (  targetData.ita_contact_hist_enrich.xcoh_state_enrich == "CT" )
                {
                %>Pre-Qualified<%
                }   
                 if (  targetData.ita_contact_hist_enrich.xcoh_state_enrich == "VA" )
                {
                %>Pre-Qualified<%
                }          
                 if (  targetData.ita_contact_hist_enrich.xcoh_state_enrich == "MN" )
                {
                %>Pre-Qualified<%
                }     
                 if (  targetData.ita_contact_hist_enrich.xcoh_state_enrich == "PA" )
                {
                %>Pre-Qualified<%
                }      
                 if (  targetData.ita_contact_hist_enrich.xcoh_state_enrich == "OH" )
                {
                %>Pre-Qualified<%
                }                                                                    
                else {
                %>Pre-Approved<%
                }
                %>

Thank you.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

vraghav
Adobe Employee
Adobe Employee
September 26, 2017

Hi Jae,

I'm not aware of an IN clause inside JavaScript.

The various IF statements you have defined is one of the correct ways to implement it but is not elegant and also not scalable.

Try defining the possible values inside an array and then make use of indexOf function,

More information here: JavaScript Array indexOf() Method

See if it helps.

Regards,
Vipul

Level 3
September 26, 2017

So I made this indexOf() script:

 

var states =["CT", "MN", "OH", "PA", "VA"];

var a = states.indexOf("CT");

var b = states.indexOf("MN");

var c = states.indexOf("NY");

...so how I do make use of this in my case?

Jean-Serge_Biro
Level 10
September 27, 2017

While If/then/else method is correct, it can lead to unreadibility if lot of tests.
So use rather what I mentioned, OR operator or even, switch statement as is:

<%

switch (targetData.ita_contact_hist_enrich.xcoh_state_enrich) {

    case "CT":

        action1tobedone

        break;

    case "VA":

        action2tobedone

        break;

    case "MN", "PA", "OH":

        action3tobedone

        break;

    default:

        action4tobedoneotherwise

        break;

}

%>

Jean-Serge_Biro
Level 10
September 26, 2017

Hi Jael,

IN operator is for SQL query where clause.

For Javascript use either operator || (OR) or switch statement to be more concise than your syntax, repeating the test in case of same action targeted. But of course your  syntax works fine as well...

Regards
J-Serge

Level 3
September 26, 2017

Hi all,

Thank you for your replies, when I develop the second option, if/Else statements, following the Adobe documentation Conditional content :  I got this error:

There is something it doesn’t like about the Else if in the bottom statement.

Why is it generating an error and what is the error pointing to?