SOLVED

Token prints out velocity variable instead of link

Go to solution
Joe_Fusaro
Level 2

Token prints out velocity variable instead of link

I created a program token, activation_link, that should pull a link from a custom Salesforce object that is synced with Marketo. The velocity script is:

#if(${Provisioning__cList.get(0).License_Code__c}=="")     

     #set($HTMLSafeString = "Please email support for your activation code" )

#else       

     #set($HTMLSafeString = $esc.html(${Provisioning__cList.get(0).License_Code__c}))

#end

${HTMLSafeString}

In my email body, I put the token {{my.activation_link}} but this prints out

${HTMLSafeString}

instead of the link, as expected. I think the value exists because it's getting to the 'else' part of the script, but it's just not rendering as expected.

What am I doing wrong?

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Token prints out velocity variable instead of link

Joe, make sure you're testing using only Preview-by-List or with a real Send Email -- not Send Sample.  The entire lead isn't loaded with a sample, so you can't test Velocity that way.

Like Mark says, don't use ${formal} notation inside a #set, as that makes it easier to make syntax errors. Use $simple notation unless you're outputting text.

I think the value exists because it's getting to the 'else' part of the script, but it's just not rendering as expected.

Not necessarily!

Because of the way Velocity handles/swallows exceptions, your first condition will be false in any of the following circumstances:

  • $Provisioning__cList is not present in the Velocity context (i.e. not checked off in the tree, or you're using a sample)
  • $Provisioning__cList exists but is not a List, so has no get method
  • $Provisioning__cList is a List with an object at index 0, but that object doesn't have a License_Code__c property
  • $Provisioning__cList is a List with an object at index 0, and that object has a License_Code__c property, but the property is an empty String

As you can see there are several code paths by which you could end up in the #else condition but not be working with the object you expect.

When are we getting that beer anyway?

View solution in original post

4 REPLIES 4
Mark_Price
Level 7

Re: Token prints out velocity variable instead of link

I would try:

- removing the formal notation aside from output

- double check that the custom object field is selected in the field tree where you input the script

Joe_Fusaro
Level 2

Re: Token prints out velocity variable instead of link

Hi Mark, What do you mean the "formal notation" ?

Are you suggesting to just include the following in the velocity script?

${Provisioning__cList.get(0).License_Code__c}

This is directly to the left of the field that I'm referencing, License Code (License_Code__c): Screen Shot 2019-01-07 at 8.24.43 PM.png

Is this what you meant with "double check that the custom object field is selected..." ?

SanfordWhiteman
Level 10 - Community Moderator

Re: Token prints out velocity variable instead of link

Hi Mark, What do you mean the "formal notation" ?

Formal is with the extra curly braces.

Simple is

$esc.html($Provisioning__cList.get(0).License_Code__c)

The reason simple is recommended is this is a syntax error

$esc.html(${Provisioning__cList.get(0).License_Code__c}.toString())

but this is not

$esc.html($Provisioning__cList.get(0).License_Code__c.toString())

So the formal notation is (obviously) not an error used totally on its own, but if you try to chain additional methods you'll get an error.

SanfordWhiteman
Level 10 - Community Moderator

Re: Token prints out velocity variable instead of link

Joe, make sure you're testing using only Preview-by-List or with a real Send Email -- not Send Sample.  The entire lead isn't loaded with a sample, so you can't test Velocity that way.

Like Mark says, don't use ${formal} notation inside a #set, as that makes it easier to make syntax errors. Use $simple notation unless you're outputting text.

I think the value exists because it's getting to the 'else' part of the script, but it's just not rendering as expected.

Not necessarily!

Because of the way Velocity handles/swallows exceptions, your first condition will be false in any of the following circumstances:

  • $Provisioning__cList is not present in the Velocity context (i.e. not checked off in the tree, or you're using a sample)
  • $Provisioning__cList exists but is not a List, so has no get method
  • $Provisioning__cList is a List with an object at index 0, but that object doesn't have a License_Code__c property
  • $Provisioning__cList is a List with an object at index 0, and that object has a License_Code__c property, but the property is an empty String

As you can see there are several code paths by which you could end up in the #else condition but not be working with the object you expect.

When are we getting that beer anyway?