SOLVED

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

Go to solution
sssingh
Level 2

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

@SanfordWhiteman @Phillip_Wild Thank you for your replies. We are following you what you are saying. We are trying to implement the second use case ( Updating the token via API, sending an email, and then updating the token before sending another email - so the personalization is done every time the email is sent to anyone. ) described by phil. We are testing this scenario but it does not seems to be working for us.

Here is what we are doing

1. create custom token pname as shown below that will be used in text token {{my.texttemplate}}

sssingh_1-1626897374994.png

2. Here is how my {{my.texttemplate}} is defined.

sssingh_0-1626972857813.png

3. Currently we are only able to use send sample functionality to send email. Here is sample of email received when testing using send sample functionality in Marketo 

sssingh_3-1626897536967.png

 

4. When sending through API (using postman) client, we are receiving below error. We have already validated the html and it comes out as valid. API call comes back with success but when we check receiver person activity log, we see below mysterious error.

sssingh_0-1626897339656.png

 

5. Here is API request through PostMan

curl --location --request POST 'https://<muchkin-number>.mktorest.com/rest/v1/campaigns/1025/trigger.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <accesstoken>' \
--data-raw '{
"input":
{
"leads" : [
{
"id" : 5
}
],
"tokens" : [
{
"name" : "order-number",
"value": "3023532055"
},
{
"name" : "pname",
"value": "Mr.Prudhvi"
},
{
"name" : "shipment-on-way",
"value": "Your order has shipped and is on it'\''s way!"
},
{
"name" : "order-status",
"value": "Partially Shipped"
},
{
"name" : "track-shipment",
"value": "9012412122"
},
{
"name" : "shipping-method",
"value": "Standard (2-Day Guaranteed)"
},
{
"name" : "packing-slip",
"value": "7569737098"
},
{
"name" : "ship-to-account-number",
"value": "98746892"
},
{
"name" : "ship-to-account-name",
"value": "chicago childcare "
},
{
"name" : "delivery-address",
"value": "121 s tonne dr, arlington heights, IL from postman"
}
]
}
}'

 

 

I will be opening support ticket to troubleshoot API error to see if we can find the root cause. FYI we are using sandbox environment for testing where we have full flexibility for testing.

 

 

Phillip_Wild
Level 10 - Community Advisor

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

I can't speak to the API call - sorry, that doesn't look like an error I'm familiar with. 

 

On the subject line part, you are using the nomenclature ${my.pname}. This is a mixture of a Velocity variable name, and a Marketo token name.

 

Velocity variable names start with a dollar sign, as you have, and can optionally have brackets - eg. ${variable}, or simply $variable. Marketo token names have the double brackets, and "my" as a prefix - eg. {{my.variable}} . 

 

In what you have in the subject line, you have mixed both of these - Velocity variable name, but with "my." in the middle as per Marketo tokens. That is likely why it isn't rendering correctly.  Hope this helps.

sssingh
Level 2

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

@Phillip_Wild Sorry I should have included the Email Draft screenshot in previous reply for context. Below is how email draft looks like . As you can see in subject line, we are not including the velocity variable but email template text token which contains velocity script that has another my token {{my.pname}}. Hope this makes sense.  I have also fixed the screenshot for {{my.texttemplate}} in previous reply.  It was not showing {{my.pname}} as token part of velocity script variable that we were printing. Third screenshot is from mobile phone where I received the email which lays it out as ${m.pname}. I am not sure why it is doing that.

sssingh_0-1626974930025.png

 

Phillip_Wild
Level 10 - Community Advisor

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

Hi @sssingh ,

 

Hmm. When you put that {{my.texttemplate}} token in the email body instead of the email subject line, does it work then?

 

I remember something I've read in the past that you shouldn't use an email script token for only PART of a subject line - it should output the full subject line. That might not be possible given you have other dynamic variables in there. But if you can get it to work in the email body, at least we have a cause.

sssingh
Level 2

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

@Phillip_Wild it is text token instead of email script token as suggested by @SanfordWhiteman earlier to in post to try instead of email script token. Here is screenshot with this text token in the body of the email which were delivered to my email id

sssingh_0-1627075181462.png

 

SanfordWhiteman
Level 10 - Community Moderator

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

  • Your Request Campaign payload is wrong; the JSON property names are like "{{my.token)}".
  • Still seems like you're trying to access my.tokens by name inside other my.tokens. This will not work. You can only access Velocity variables #set in one token inside code in another token.
sssingh
Level 2

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

@SanfordWhiteman I am trying to understand your first point where you say "Your Request Campaign payload is wrong; JSON property names are like {{my.token)}". I am not able to understand. Can you please be more specific in API request payload where we are doing that ?  

- understood your second point. Based on one of your previous reply in this post, you mentioned you can use velocity script in text token. I think we misunderstood your answer there. I have to say though that without being able to pass any type of data to velocity script , its usage will be very limited. Especially if that data is not associated with any objects that are supported in email script. As you can see we are trying to layout html tabular structure in the email body with order items details. We are passing those order items through API invocation. Order item count will vary from one API call to another. we will have to put some sort of loop construct to iterate over those items to create table html. Any idea how to handle such scenario in your previous experience? 

SanfordWhiteman
Level 10 - Community Moderator

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


wrong; JSON property names are like {{my.token)}". I am not able to understand. Can you please be more specific in API request payload where we are doing that ?  

- understood your second point.


Right here:

{
  "name" : "order-number",
  "value": "3023532055"
}

 

The syntax is actually:

{
  "name" : "{{my.order-number}}",
  "value": "3023532055"
}

 

 


I think we misunderstood your answer there. I have to say though that without being able to pass any type of data to velocity script

But I never said you couldn’t “pass any type of data.“

 

I said you couldn’t reference Lead, Custom Object, or Opportunity fields in the code that’s inside the text token, for the obvious reason that there’s no place to check off the fields that should be injected into the Velocity context. The Text {{my.token}} itself can contain locally set variables.

 

(In fact it’s more complex than the above, you can reference fields if they’re checked off in an earlier token, but you can’t if your Text {{my.token}} is the first token in the email.)

sssingh
Level 2

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

 

@SanfordWhiteman 

Thank you for the example. That made it very clear. I was hoping that you can answer my second question around accepting data through API call which can be dynamic nature and laying that out in email using velocity script. We will be editing email template html directly. I know you said that Text {{my.token}} itself can only contain locally set variables but that does not help
us with passing that data to this token which was passed through API invocation request payload. Lets say I am passing following data from API call


API payload token value with json string

{
"name":"{{my.json-data-token}}"
"value":"{
{"itemId":"123", "qty":"10", "unit":"box", "sku":"sku787"},
{"itemId":"124", "qty":"1", "unit":"case", "sku":"sku788"},
{"itemId":"125", "qty":"11", "unit":"box", "sku":"sku789"},
{"itemId":"126", "qty":"5", "unit":"single", "sku":"sku790"},
{"itemId":"127", "qty":"6", "unit":"box", "sku":"sku791"}
}"
}

 

Email Template Body Html Segment that parses json data and create table segment in the email template body

<table>
## parse data from json-data-token as array of objects, parse function is madeup for demonstration
# set $json-data-token-list = $util.parseJson({{my.json-data-token}})
## iterate over json-data-token passed through API call
#foreach( $item in $json-data-token-list )
## construct the html for table row and output that into email content
#set( $table_row_html = "<tr><td>"+ $item.itemId +"</td><td>"+ $item.qty +"</td><td>"+ $item.unit +"</td><td>"+ $item.sku +"</td></tr>")
$($table_row_html)
#end
</table>

 

Let me know if something like this can work. Above is just pseudo code and real code example. I just want to know feasibility of it before going down this path.

SanfordWhiteman
Level 10 - Community Moderator

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

There’s no method $util.parseJson, don’t know where you’re getting this from.

 

It still seems like you don’t understand the approach.

 

Once again: do not send raw JSON as the {{my.token}} value.

 

Send VTL, including #set(), as the {{my.token}} value.

 

Thus as far as Velocity knows, you’re passing a #set statement that creates a LinkedHashMap (with nested LinkedHashMaps). You‘re not passing a JSON string (it is never parsed as JSON, even if it some of it looks like JSON),

 

If this still doesn’t make sense, you should wait for me to blog about it at greater length.