IF and Else if statement | Community
Skip to main content
May 26, 2016
Question

IF and Else if statement

  • May 26, 2016
  • 3 replies
  • 4861 views

Using velocity script and custom object I'm trying to script an IF and Else if statement with multiple conditions using (&&) and (!=). Here is a sample of the code.

I'm getting the content to render correctly for when it meets one of the condition (when variable.variableName == A OR == B) but I can't get it to render right for when variable.variableName equals to both A AND B. So pretty much the last condition is not working and I can't get it to show both A and B contents. Any ideas? Thanks!

               #foreach($variable in $variable_cList)
               #if(${variable.variableName} == "A" && ${variable.variableName} != "B")
               #set($matched = ${variable.variableID})
             
               Show content A

               #elseif(${variable.variableName} != "A" && ${variable.variableName} == "B")
               #set($matched = ${variable.variableID})
              
               Show content B
             
               #elseif(${variable.variableName} == "A" && ${variable.variableName} == "B")
               #set($matched = ${variable.variableID})
              
               Show A and B contents

               #else
               #end                                                
               #end

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

3 replies

Kenny_Elkington
Adobe Employee
Adobe Employee
May 26, 2016

A variable can never be equal to two different values, so the third statement can never be true.  Do you have a real example of the values you're trying to evaluate here?

June 3, 2016

Thanks Kenny. Good to know that a variable cannot equal to two different values, that could explain why it didn't work. Then how can I code up something for when I want something to render when both conditions are true?

I'm pulling from a custom object data call "competency".

Values are competency values such as :  Cloud Platform, SMCS, etc.

So I want a block of content to render if someone has a competency values of  both "Cloud Platform" and "SMCS".

Ulf_Deeg
Level 3
June 6, 2016

I guess you want to check if the field CONTAINS A, B etc? Not Equals A, B, etc.  Right?

#set ($string = ${competency.field})

#set ($RegEx = "RegularExpressionHere")

#if($string.contains("value"))

   foo

#elseif($string.matches($RegEx))

bar

...

Nicholas_Manojl
Level 8
May 26, 2016

#foreach($variable in $variable_cList)
#if(${variable.variableName1} == "A" && ${variable.variableName2} != "B")
#set($matched = ${variable.variableID})
             
<p>Your HTML - Show content A</p>

#elseif(${variable.variableName1} != "A" && ${variable.variableName2} == "B")
#set($matched = ${variable.variableID})
              
<p>Your HTML Show content B</p>
             
#else(${variable.variableName1} == "A" && ${variable.variableName2} == "B")
#set($matched = ${variable.variableID})
              
<p>Your HTML Show A and B contents</p>

#else

#end                                                
#end

--

I'm not an expert (far from it) so probably you'll get a better answer elsewhere. But my understanding is that "else" refers to the first #if and #elseif being unmatched. Otherwise I think you just make everything an #if.

Ulf_Deeg
Level 3
June 7, 2016

ok. so you loop through a list, right?

then I guess you need to add a counter or false/true of some sort.

hasA = 0

hasB = 0

hasC = 0

for each A

  hasA = hasA +1

for each B

  hasB = hasB +1

for each C

  hasC = hasC +1

=> in the end check for the variations and define the content. e.g. hasA <> 0 && hasB = 0 && hasC =0 ...

June 15, 2016

Yes it does loop through a list. Cool, I'll give it a try! Thank you!