SOLVED

TOKEN SCRIPT NOT WORKING!

Go to solution
DeAnna_Humphrey
Level 2

TOKEN SCRIPT NOT WORKING!

I have a token script that is working in one workspace but not in another. The script for whatever reason is pulling the value of a different Salesforce field. See screenshots below to see the error.

Here is the script token I am using:

## First, covert the variable to currency format

#set($total = $number.currency($$$${lead.Bonus_Gift_Card_Amt__c})) ##our code from before, e.g., $121,237.10

#set($stringLength = $total.length() - 3) ##get the total string length and calculate what we want to keep, e.g., 8 --note the specific spacing between the commands here! screwing up the syntax will crash it (e.g. if you remove the space)

#set($totalb = $total.substring(0,$stringLength)) ##takes the sub string of just the first 3 characters, e.g., $121,237

## Then, for the email output

${totalb}

1 ACCEPTED SOLUTION

Accepted Solutions
J_Grant_Gray
Level 4

Re: TOKEN SCRIPT NOT WORKING!

14 REPLIES 14
SanfordWhiteman
Level 10 - Community Moderator

Re: TOKEN SCRIPT NOT WORKING!

Please

1. Highlight the code using the Advanced Editor's syntax highlighter so it's readable.

2. Include the images directly in the post, not as attachments. Not all Community users can see attachments and it's very confusing.

DeAnna_Humphrey
Level 2

Re: TOKEN SCRIPT NOT WORKING!

## First, covert the variable to currency format

#set($total = $number.currency($$${lead.Bonus_Gift_Card_Amt__c})) ##our code from before, e.g., $121,237.10

#set($stringLength = $total.length() - 3) ##get the total string length and calculate what we want to keep, e.g., 8 --note the specific spacing between the commands here! screwing up the syntax will crash it (e.g. if you remove the space)

#set($totalb = $total.substring(0,$stringLength)) ##takes the sub string of just the first 3 characters, e.g., $121,237

## Then, for the email output

${totalb}

This is the only way I can see how to copy and paste it.

SanfordWhiteman
Level 10 - Community Moderator

Re: TOKEN SCRIPT NOT WORKING!

Choose "Use Advanced Editor" and...

syntax_highlighter.gif

SanfordWhiteman
Level 10 - Community Moderator

Re: TOKEN SCRIPT NOT WORKING!

Also, I don't know what you mean by a different field. There's a specific field in the code and that has to be the same field selected in the tree in Script Editor.

J_Grant_Gray
Level 4

Re: TOKEN SCRIPT NOT WORKING!

Screen Shot 2019-02-11 at 2.16.15 PM.png

Nicholas_Manojl
Level 9

Re: TOKEN SCRIPT NOT WORKING!

You need two scripts.

Script A is for your interest savings, and it references the interest savings field.

Script B is for your gift card amount, and it references the gift card field.

Your email will then be able to insert {{my.interest savings}} and {{my.gift card}} as two separate tokens.

I reckon you have found 'script B' but when you look at your email, you will see that it is referencing 'script A' (twice).

SanfordWhiteman
Level 10 - Community Moderator

Re: TOKEN SCRIPT NOT WORKING!

I'm going to copy, paste, and highlight your code myself so I can correct your syntax. Please highlight in future posts.

#set($total = $number.currency($$$${lead.Bonus_Gift_Card_Amt__c})) ##our code from before, e.g., $121,237.10

This is not the correct way to use dollar signs in Velocity. The only reason it happens to work is that Velocity ignores multiple $ in a row (undocumented behavior; the VTL parser is currently loose in this way, though it may not be in the future). And you also should be using $simple notation, not ${formal}, as discussed in many other posts.

So the syntactically correct line is

#set( $total = $number.currency($lead.Bonus_Gift_Card_Amt__c) ) ##our code from before, e.g., $121,237.10

You have another overly fragile line here (as your comment says):

#set($stringLength = $total.length() - 3) ##get the total string length and calculate what we want to keep, e.g., 8 --note the specific spacing between the commands here! screwing up the syntax will crash it (e.g. if you remove the space)

But that fragility ("screwing up the syntax will crash it") is because you're not subtracting using the right syntax, which is

#set( $stringLength = $math.sub($total.length(), 3) )

That won't crash just by removing whitespace.

Perhaps most important, the whole subtract-from-length-and-truncate logic is unnecessary. You only needed one line. Cast to Integer, which takes the floor of the number, then supply a number format:

#set($total = $number.format('$###,###', $convert.toInteger($lead.Bonus_Gift_Card_Amt__c)))

This will turn

121237.10 ⇒ $121,237

121237.90 ⇒ $121,237

etc.

DeAnna_Humphrey
Level 2

Re: TOKEN SCRIPT NOT WORKING!

Sanford,

You are awesome and I appreciate your help. However, you assume I know the

code. I do not. I am learning as I go. So your input is helpful for me

but the way you show me in your how to steps doesn't make complete sense to

me. The original script was what was given to me a while back and it was

working fine in one of my workspaces and when I copied and pasted it to my

second workspace it as you said above crashed.

Thanks again for all your assistance and in the future realize not everyone

working in Marketo knows how to work with the code.

Best,

DeAnna

On Mon, Feb 11, 2019 at 8:40 PM Sanford Whiteman <

SanfordWhiteman
Level 10 - Community Moderator

Re: TOKEN SCRIPT NOT WORKING!

Well, if you don't understand what code is supposed to do at all, it's impossible to explain to others the difference between the desired/expected result and the observed (real-world) result.

But it doesn't take any understanding of the code to at least highlight it properly so people -- including the visually impaired -- can read it and try to reason through it. That's just best practice when looking for help from a Community.

My point was that the code is unnecessarily fragile + difficult for others to understand (and uses broken syntax that works more out of luck than anything else). It's more stable + easier to understand if you use only the last line I provided. The bulk of the Velocity code was ultimately unnecessary.

As Nicho points out, you may primarily be confused about how many tokens you have in the other workspace. I don't know, as I still can't see your images because you didn't paste them inline. Look closely at the emails in the other workspace, check the names of the {{my.tokens}} in the email, and find them in the Marketing Activities tree.