Highlighted

Dynamic coding with multiple values

jael91407431

26-09-2017

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.

Replies

Highlighted

Vapsy

Employee

26-09-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

Highlighted

Jean-Serge_Biro

MVP

26-09-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

Highlighted

jael91407431

26-09-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:

errro.jpg

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?

Highlighted

jael91407431

26-09-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?

Highlighted

Jean-Serge_Biro

MVP

27-09-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;

}

%>

Highlighted

Jean-Serge_Biro

MVP

27-09-2017

Hi Jael,

If you need to compare with dynamic values, in such case it is far better to use arrays technique as Vipul recommended.
For instance, if you load all the states from a queryDef on Country schema into an array, or better, an object (with name, ISO2, ISO3 fields), you can use easily with the typeOf or even directly by the indexing array / object to get the element.
And this technique is mandatory in case of for each loop.

Regards
JS

Highlighted

jael91407431

02-10-2017

Thank you so much Jean-Serge! Sorry I didn't reply earlier, as for some reason, my Outlook is filtering my replies to Adobe Campaign forum. But will implement your suggestion! I look forward to your replies with relish!!

Highlighted

Raaghuik1996

17-06-2020

Hi @Jean-Serge_Biro ,

 

I am trying to use switch case as shown above, my requirement is like I have a dynamic content which needs to be shown based on the state code which I store it in recipient table

I am using the below code snippet , but I cannot see blank data instead of either Abc or fed....Please help

<%
switch(recipient.location.stateCode){
case 'IL':
console.log("Abc");
case 'KS':
console.log("fed");
}
%>