SOLVED

Re: HELP : Conditional Scripting

Go to solution
nsildeyna
Level 1
Guys, i need help
#set($testA = "{{lead.change}}") **content is down
#set($testB = "Down")
#if( $testA == $testB )
TRUE OUTPUT
#else
FALSE OUTPUT $testA $testB
#end
when rendered output is FALSE OUTPUT Down Down
Question : why it is not rendering "TRUE OUTPUT" ?? while $testA and $testB is the same "Down"
1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

That’s not what it should look like. Open a P1 ticket immediately, I wouldn’t trust any instance with this behavior to be using tokens correctly.

 

In proper operation, Script Editor works like this:

Note I removed the formal notation (curly braces) as that’s how you should start working with variables on the canvas. Formal notation is only necessary for output and otherwise can cause unexpected errors.

View solution in original post

16 REPLIES 16
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

For the future, please use the code highlighter to highlight the code in your posts. I edited your post this time. You can't use the token reference to assign values/use its data in the velocity script. You need to reference the fields as per the velocity construct (using the "$" character). Also in general you should use .equals() to check for equality instead of "==" as the former is more robust.

 

#set($testA = $lead.Change)
#set($testB = "Down")
#if($testA.equals($testB))
True Output
#else
False Output
#end

 

Also, instead of assigning the value of the change field to the $testA variable first, you can directly use it to check whether it’s equal to the value in the $testB field or not (and even just directly check for equality between the $lead.change field and “Down” value instead of assigning “Down” value to the $testB variable altogether).

 

#if($lead.Change.equals("Down"))
True Output
#else
False Output
#end

 

Make sure that you pull the person field used in the velocity editor from the object tree on the right, and the little checkbox marked near the field name is also checked.

Darshil_Shah1_0-1694956553570.png

nsildeyna
Level 1

Hi @Darshil_Shah1 thanks a lot for the reminder and reference. 
tried this as per your direction :

#set($testA = "{{lead.belonging_change}}") 
#if($testA.equals("Down")) 
  OUTPUT A
#else 
  OUTPUT B $testA
#end

but it still renders 

OUTPUT B Down




Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

In my comment above, did you read the part where I mentioned using the velocity reference ($) instead of the token reference while referencing fields? Please drag the field from the object tree (per the last snapshot) into the velocity editor instead of trying to reference it through the lead token format. Also, you don't need quotes ("") while assigning a velocity variable with the value in the field. Please reference the scripts in my previous comment.

nsildeyna
Level 1

Thanks for responding and bare with me, im pretty new in Marketo 🙂
this is whats available on the right of the template. 

nsildeyna_0-1694975957784.png

this is what shows after i dragged it from the right
{{$lead.Belonging Increase}}
and renders as 
Down

using $lead.Belonging Increase returns nothing

 

but again, what confuses me is we already got the Down from that token. but when tried

#set($testA = {{lead.Belonging Increase}})
#if($testA.equals("Down")) 
 OUTPUT A
#else
 OUTPUT B
#end

it renders OUTPUT B
while it should renders OUTPUT A

should we convert it to string first?
Really appreciate your help. 

 

SanfordWhiteman
Level 10 - Community Moderator

It's impossible for the Velocity name of a field to have spaces.

 

So you cannot have dragged a field with this name from the tree in Script Editor.

 

(It also would never have double curly braces around it.)

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Good point, Sandy! @nsildeyna, when we say drag, we literally mean it (not just copying its as-is name from the tree), i.e., you should click the field in the object tree, hold, and drag the field into the editor. See the snapshot below for reference.

Darshil_Shah1_0-1695009447011.png

Jo_Pitts1
Level 10 - Community Advisor

@Darshil_Shah1, don't forget that dragging the field into the editor from the tree on the RHS always uses formal notation, which should largely be avoided (see @SanfordWhiteman's excellent blog here https://blog.teknkl.com/strive-to-avoid-velocity-formal-references/). 

 

@nsildeyna, the other thing to note is that when you drag and drop a field from the tree to the editor, it doesn't drop it where your mouse pointer ends up, but where the text editing cursor is.  That got me a few times back in the day!

 

Cheers

Tags (1)
nsildeyna
Level 1

Good to know! Thanks @Jo_Pitts1 

the primary question rn maybe,
why it didnt catch the conditional logic while both are Down ?

should we normalize it to string first?

Jo_Pitts1
Level 10 - Community Advisor

@nsildeyna ,

Can you paste in the current version of your code.  I'm not sure exactly what it looks like currently.

Can you also confirm you have the field you are using ticked in the tree on the RHS.

Cheers

Jo

 

SanfordWhiteman
Level 10 - Community Moderator

why it didnt catch the conditional logic while both are Down ?

should we normalize it to string first?


No such thing as normalizing to a String. All lead fields are Strings in Velocity.

nsildeyna
Level 1

Hi Guys, it is as is from the list on the right.

nsildeyna_0-1695011452635.png

this when rendered emits "Down" but the problem it didnt catch on the conditional.
but if you put it manually like so : 

#set($testA = "Down"}
#if($testA.equals("Down"))                                       
  OUTPUT A
#else 
  OUTPUT B 
#end

It renders OUTPUT B

 

SanfordWhiteman
Level 10 - Community Moderator

What you’re describing is still not possible unless there is a gigantic bug in your instance alone, which I doubt.

 

Dot-properties ($lead.property syntax) cannot have spaces. This will create a fatal error in Velocity.

 

Please post a screen recording of you dragging the field from the tree to the canvas.

nsildeyna
Level 1

12-15-13.gif

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Woah- never seen this! @nsildeyna, as Sandy says, you should open a P1 support ticket! I'd also include this video so that they're clear as to what is happening.

SanfordWhiteman
Level 10 - Community Moderator

That’s not what it should look like. Open a P1 ticket immediately, I wouldn’t trust any instance with this behavior to be using tokens correctly.

 

In proper operation, Script Editor works like this:

Note I removed the formal notation (curly braces) as that’s how you should start working with variables on the canvas. Formal notation is only necessary for output and otherwise can cause unexpected errors.

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

For velocity to be able to parse and assign values you must use the $ reference and and not the token reference bein used elsewhere (e.g., emails, landing pages, etc). Use the below script (of course with the "Belonging Increase" field selected in the object tree on the right of the velocity editor):

 

#set($testA = $lead.BelongingIncrease)
#if($testA.equals("Down"))
 OUTPUT A
#else
 OUTPUT B
#end

 


but again, what confuses me is we already got the Down from that token. but when tried 

That seems to be a different field than you have added in the latest comment (belonging_change vs. Belonging Increase). Are you sure you didn't have the person token added to the email that might have displayed the "Down" apart from the velocity token you're working on?