Expand my Community achievements bar.

SOLVED

Dynamic coding with multiple values

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Employee

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

9 Replies

Avatar

Employee

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

Avatar

Level 3

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?

Avatar

Level 10

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;

}

%>

Avatar

Level 2

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");
}
%>

Avatar

Level 2

sorry small correction that I can see blank data instead of Abc or fed which i am trying to display

Avatar

Level 10

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

Avatar

Level 3

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!!

Avatar

Level 10

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

Avatar

Level 3

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?