How to Use Message Templates to Tackle Complex Data Structure
What are Message Templates in Workato?
Message templates in Workato allow you to create static templates for commonly used messages. It can be used to generate HTML/text/JSON/XML messages.
Related: Message Templates Documentation
Why are messaging templates important?
When building automations, you often need to take a complex data structure and transform it into a single text document. One obvious example of this type of transformation is mass personalized emails.
Related: Activate Unstructured Data
How does mass personalized emails use message templating
An email platform takes semi-structured data about a customer — first and last name, title, contact info, recent purchases — and generates a personalized document. It seems simple, but to do this, you need a template that can apply conditional logic to make decisions. For example:
- Start the email with “Hi Jim!” if the first name is known, or “Hi there!” if it isn’t.
- Include a summary of a recent purchase which might be a single product, or a dozen.
- Advertise an in-person event based on the customer’s location.
Related: Semi-structued Data Tamed With Javascript
How to use Workato’s Message Templates
It’s not just marketing emails, though. Any time you need to turn a complex data model into something a human can easily understand, a templating tool can help.
For another example, let’s revisit our most recent tutorial on custom actions. In that scenario, we captured leads from a webinar and created them in Salesforce. We also created a custom field in Salesforce to capture any questions the prospect asked during the webinar:
It looks easy, but let’s have a closer look at the data we’re working with:
It’s lists within lists! A prospect might ask any number of questions, or none at all. In turn, each question can have more than one answer. To store this information in Salesforce, we need to transform it into a single, human-readable block of text.
Let’s look at how to tackle this using Workato’s Message Templates feature.
Step 1: Create a message template
First we find the Message Templates page in the left nav under Tools and click New Template:
The template has two parts:
- Template input
- Template body
Step 2: Fill in template input
The input is defined as a JSON schema. If you’re not comfortable writing JSON, you can often get started by copying a sample from a test job. In this case, the output of our Get questions from session action matches the input we need for the template.
Related: Message Templates For Complex XML
First paste the sample into the wizard:
Optionally, we can clean it up a little to remove fields that we won’t need and make some names more readable:
Step 3: Writing the message template
Now that we’ve defined the input, we need to write the template. We want each question to look something like this:
Question:
What’s the best Halloween candy?
Answers:
Reece’s Peanut Butter Cups, obviously!
Answered by Larry
Snickers bar. Full size, or nothing.
Answered by Moe
Message templates use a standard templating language called Mustache that lets us use variables and conditional logic to create the document. You can read the full docs for Mustache here.
Defining a repeated block
We start by defining a block that we want to repeat for each question in our questions list. This is done by beginning the block with a pound “#” and ending it with a slash “/”.
In this case:
{{#questions}}
{{/questions}}
The template outputs plain text, so you can use it to create documents formatted in markdown, HTML, etc. The Salesforce field that we will eventually map this document to accepts HTML tags. Thus, we will use those to format our message.
Adding a variable
We’ll start with the question. To include a variable from our input, place it in the template surrounded with double curly braces like {{this}}:
Remember that each question can have more than one answer, so we need to nest another repeating block inside the main one to build out each answer:
Step 4: Use message template in a recipe
Our message template is now complete! To use it in our recipe, we can use the Message Templates by Workato app:
Find the correct message template in the picklist:
Map the correct datapills to the message template’s input:
Related: How to group map your data
The output of this action is a single document we can map to the Webinar Questions field in Salesforce:
Results!
Our message template lets us map our list of webinar questions down to a single document. It works no matter how many questions are in the list, and no matter how many answers each question has!