SOLVED

Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

Go to solution
Patrick_Vincler
Level 2

Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

So have been stumped on this one for a while and thought to reach out.  Per Sanford Whiteman, I've tried avoiding formal notation and have used .equals vs ==.  Have also ensured the boxes are checked in the selector panel.

This version only displays the default value after "#else".  Thanks for any ideas here.

#if ( $lead.SavingsTier.equals("Low") && $lead.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_Low}")
#elseif ( $lead.SavingsTier.equals("ExtraLow") && $lead.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_ExtraLow}")
#elseif ( $lead.SavingsTier.equals("Medium") && $lead.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_Medium}")
#elseif ( $lead.SavingsTier.equals("High") && $lead.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_High}")
#elseif ( $lead.SavingsTier.equals("Full") && $lead.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_Full}")
#else
Save Big
#end
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

But I am wanting it to instead to print the data within the token:

 

15%

You can't do that by bringing in external (presumably Text) token names.

You have to create a Velocity {{my.token}} with contents like:

#set( $Aruba_Savings = {
"Low" : "15%",
"High" : "30%"
} )

Then as long as you include that {{my.token}} above the {{my.token}} that does the output, you can directly reference the Map in the second token:

$Aruba_Savings[$lead.SavingsTier]

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

I don't see anything amiss, though I am on my phone.  Are you sure you're testing correctly -- that is, real email or preview-by-list?

(Not sure what you want to happen with those {my.thingies} within VTL, though.)

Patrick_Vincler
Level 2

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

Yeah I'm testing this through firing a live email through the campaign. My hunch is that I may be asking velocity to to something that is not possible. 

Desired results:

When both of the conditions are met in any of the IF statements, the correct custom token will be referenced and the value within it displayed in the body of email where this scripting token is placed.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

You can't access data from non-Velocity tokens inside Velocity.

You can easily set $references in other Velocity tokens and use them in a later tokens (IME, Velocity will drive you insane if you don't take advantage of this!).

But what you're doing here isn't trying to reference non-Velocity tokens using any nomenclature used in Marketo.  That's why I deliberately called them "thingies" because single curly braces, without dollar signs, don't mean anything anywhere in Marketo. They're just strings that happen to have a couple of curly braces.

None of this explains your observation that you only see the default value.  As you will see if you try this code, the way you've written the condition works fine:

#set( $myMap = {
"SavingsTier" : "Low",
"LastCitySearched" : "Aruba"
})
#if ( $myMap.SavingsTier.equals("Low") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_Low}")
#elseif ( $myMap.SavingsTier.equals("ExtraLow") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_ExtraLow}")
#elseif ( $myMap.SavingsTier.equals("Medium") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_Medium}")
#elseif ( $myMap.SavingsTier.equals("High") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_High}")
#elseif ( $myMap.SavingsTier.equals("Full") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{my.Aruba_Savings_Full}")
#else
Save Big
#end
${savings}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This will print

{my.Aruba_Savings_Low}‍

as expected, since it's just a String. 

It won't print

Save Big‍

which is what you seem to say you're seeing.

Patrick_Vincler
Level 2

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

My bad. I had forgotten to double brace my tokens in the example. So it should look like this below. This does print 

{{my.Aruba_Savings_Low}}

but I am wanting it to instead to print the data within the token:

15%

#set( $myMap = {
"SavingsTier" : "Low",
"LastCitySearched" : "Aruba"
})
#if ( $myMap.SavingsTier.equals("Low") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{{my.Aruba_Savings_Low}}")
#elseif ( $myMap.SavingsTier.equals("ExtraLow") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{{my.Aruba_Savings_ExtraLow}}")
#elseif ( $myMap.SavingsTier.equals("Medium") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{{my.Aruba_Savings_Medium}}")
#elseif ( $myMap.SavingsTier.equals("High") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{{my.Aruba_Savings_High}}")
#elseif ( $myMap.SavingsTier.equals("Full") && $myMap.LastCitySearched.equals("Aruba") )
#set($savings = "{{my.Aruba_Savings_Full}}")
#else
Save Big
#end
${savings}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

My goal is to show users the appropriate level of savings in the email when they a) qualify for that tier of savings, and b) they have the appropriate city record for the savings, both of which are tied to the lead's file. Hope I am making sense. 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

But I am wanting it to instead to print the data within the token:

 

15%

You can't do that by bringing in external (presumably Text) token names.

You have to create a Velocity {{my.token}} with contents like:

#set( $Aruba_Savings = {
"Low" : "15%",
"High" : "30%"
} )

Then as long as you include that {{my.token}} above the {{my.token}} that does the output, you can directly reference the Map in the second token:

$Aruba_Savings[$lead.SavingsTier]
Patrick_Vincler
Level 2

Re: Velocity - Conditionally Rendering A Custom Token Based on Two Factors per Condition

Ok makes sense. I'll need to figure something out. They are in fact text tokens, but ones generated and regularly updated via API.