SOLVED

Re: Email Script Token Input from tokens using API trigged email invocation

Go to solution
sssingh
Level 2

Email Script Token Input from tokens using API trigged email invocation

I am trying to pass json data input to email script token from Marketo API invoked Email notification using tokens so that layout data in table format in Email Message Body. I know we can use lead/contact object attributes in addition to other objects like Oppurunity or Account but I have not seen example where we use one custom token ({{my.jasonData}}) into Email Script token so that Velocity script can be used to create html content that can be renderded. Any ideas/suggesstion if this possible and how to do it(with example)

1 ACCEPTED SOLUTION

Accepted Solutions
sssingh
Level 2

Re: Email Script Token Input from tokens using API trigged email invocation

@SanfordWhiteman After your below reply (Send VTL, including #set(), as the {{my.token}} value.), We got the idea that we can  pass VTL (velocity template language) from API as token content along with data. By referring to your blog post , we were able to pass json data (see texttemplate variable below) from API itself with VTL included to parse and layout that json data. Here is how final request looks like 

 

 

curl -X POST \
https://<munchkinid>.mktorest.com/rest/v1/campaigns/1025/trigger.json \
-H 'Accept: */*' \
-H 'Authorization: Bearer <token>' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-H 'Host: <munchkin-id>.mktorest.com' \
-H 'Postman-Token: <postman-token>' \
-H 'User-Agent: PostmanRuntime/7.15.0' \
-H 'accept-encoding: gzip, deflate' \
-H 'cache-control: no-cache' \
-H 'content-length: 1591' \
-d '{
"input": {
"leads": [
{
"id": 2
}
],
"tokens": [
{
"name": "{{my.texttemplate}}",
"value": "<table border=1><tr><th>Email Id</th><th>Full Name</th><th>Date Referred</th><th>Referral Program</th></tr> #set($jsonString = '\''[{\"Email\" : \"joe@example.com\",\"FullName\" : \"Joe Exton\",\"DateReferred\" : \"2016-08-01\",\"ReferralProgram\" : \"Season Pass Holders\"},{\"Email\" : \"jill@example2.com\",\"FullName\" : \"Jill Towson\",\"DateReferred\" : \"2016-08-03\",\"ReferralProgram\" : \"Gold Donors\"}]'\'') #set( $jsonData = '\''#set( $jsonData = '\''+$jsonString+'\'')'\'') #evaluate($jsonData) #foreach($jdata in $jsonData)<tr><td >${jdata.Email}</td><td >${jdata.FullName}</td><td >${jdata.DateReferred}</td><td >${jdata.ReferralProgram}</td></tr> #end </table>"
},
{
"name": "{{my.order-number}}",
"value": "3023532055"
},
{
"name": "{{my.shipment-on-way}}",
"value": "Your order has shipped and is on it'\''s way!"
},
{
"name": "{{my.order-status}}",
"value": "Partially Shipped"
},
{
"name": "{{my.track-shipment}}",
"value": "9012412122"
},
{
"name": "{{my.shipping-method}}",
"value": "Standard (2-Day Guaranteed)"
},
{
"name": "{{my.packing-slip}}",
"value": "7569737098"
},
{
"name": "{{my.ship-to-account-number}}",
"value": "98746892"
},
{
"name": "{{my.ship-to-account-name}}",
"value": "chicago childcare hospital"
},
{
"name": "{{my.delivery-address}}",
"value": "121 s tonne dr, arlington heights, IL from postman"
}
]
}
}'

 

View solution in original post

22 REPLIES 22
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Input from tokens using API trigged email invocation

You can’t read another Text {{my.token}} as plain text from a Velocity {{my.token}}.

 

But you can write inline Velocity in a Text {{my.token}} — it cannot have any dependencies, that is, it can’t reference any lead or object fields — that #sets a variable in the context, then read that variable from a Velocity {{my.token}} located after it in the email.

sssingh
Level 2

Re: Email Script Token Input from tokens using API trigged email invocation

@SanfordWhiteman Are you suggesting that I can just put velocity script inside html content without using Email Script token to use Velocity script inside my email template. That is how I read you comment. I will try and revert back to you if this works.

sssingh
Level 2

Re: Email Script Token Input from tokens using API trigged email invocation

@SanfordWhiteman we tried your suggestion but it seems  to be giving error. Below screenshots shows that

1. first screenshot gives you error overview when you insert Velocity script directly inside your Email Html

2. Second screenshot is showing  text token value that contains velocity script as you suggested.

3. Third screenshot is showing how we insert the text token (containing velocity script) in Email html code.

4. Fourth screenshot shows when you save the changes and error shows up in Email preview screen about inject text token with velocity script.

Hope below is clear. Let me know if there is any confusion. Thanks for you help.

no-variable-with-id-declared.PNGtext-token-with-velocity-script.PNGText-token-with-velocity-script-in-html.PNG

velocity-script-errot-with-text-token-having-velocity-script.PNG

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Input from tokens using API trigged email invocation

I didn’t say anything about running #evaluate directly in email content. The error is not surprising because Marketo sees the formal reference as a ${mktoVariable} (which is no coincidence).

 

You can include Velocity in a Text {{my.token}} but of course the Velocity needs to be error-free. I couldn’t say, without analyzing the entirety of your code and approach, why you’re getting that lexical error.

sssingh
Level 2

Re: Email Script Token Input from tokens using API trigged email invocation

@SanfordWhiteman Contents of text token are shown in screenshot.  It is just setting velocity variable with simple constant string value. When we inject that text token, we see the error. When we remove the text token, We do not see the error. Is that not sufficient to conclude that issue is velocity script in text token. I am happy to email you html code if you have where I need to send the code.

 

Phillip_Wild
Level 10

Re: Email Script Token Input from tokens using API trigged email invocation

Hi @sssingh ,

 

Based on what I've read above, I feel that if you simply change your "evaluate" line to output the variable instead: 

#set($greeting = "Dear Mr. Dan")
$greeting

 

Then you can do what you are setting out to do.  This worked in my testing - I created a text token which contained the full code above, and then placed that text token in the email as {{my.Test Greeting}}. When previewing the email as a specific person, it showed the results of the Velocity code.

 

So to expand upon this, you could update that plain text token with the Velocity code you want to run, and that will modify the email. But as Sanford mentioned, you can't use any variables related to the person in this way, so you are either:

 

1. Sending everyone the same content - which defaults the purpose, right? Or:

2. Updating the token via API, sending an email, and then updating the token before sending another email - so the personalisation is done every time the email is sent to anyone. 

 

Overall, this is a bit of a hack, but it seems like @SanfordWhiteman 's solution might work for you. 

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Input from tokens using API trigged email invocation


Based on what I've read above, I feel that if you simply change your "evaluate" line to output the variable instead: 
#set($greeting = "Dear Mr. Dan")
$greeting

Correct. There’s no need for #evaluate. The code is already compiled as code the first time, not just as text.

prudhvivemana
Level 1

Re: Email Script Token Input from tokens using API trigged email invocation

#set($greeting = "Dear Mr. Dan")
$greeting

 

#set($greeting = "Dear " + {first-name})

$greeting

 

first-name will be Mr. Dan from api call and its another text token we created (accessible by using {{my.first-name}} in the HTML).
Is this possible ? 

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Token Input from tokens using API trigged email invocation

Your question isn’t exactly clear, but Velocity in a Text {{my.token}}, not just in an Email Script {{my.token}}, will be compiled.

 

Of course one would not usually deliberately put Velocity in a Text {{my.token}}, because (a) it’s confusing and (b) it doesn’t allow you to export lead/oppty/CO fields into the context... but it can be useful when you’re updating using the API, since only one of the {{my.token}} types can be updated that way.

 

Not sure if there’s any other way to phrase the above, Phil and I have said it a few times already...