Workato Hacks

How to Use Message Templates to Tackle Complex Data Structure

Read Time: 5 minutes

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:

Custom field in SalesForce to capture webinar questions

Custom field in SalesForce to capture webinar questions

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:

message templates in workato

Click “New Template”

The template has two parts:

  • Template input
  • Template body
template body and input

Top: Template body, Bottom: Template input

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

JSON for template input

Copy JSON from output of “Get Question From Session” action into template input

First paste the sample into the wizard:

Paste JSON sample into input

Paste JSON sample into input

Optionally, we can clean it up a little to remove fields that we won’t need and make some names more readable:

clean up JSON

Remove unnecessary fields, edit names to be 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}}

defining a block in message template

Defining a block we want to repeat

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}}:

input variable into message template

include the variable “question” from our input into the template

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:

nest repeating block in message template

Nest repeating block in main block

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:

Message Template by Workato app

Message Template by Workato app

Find the correct message template in the picklist:

webinar question message template

Select “Webinar Question” message template

Map the correct datapills to the message template’s input:

Related: How to group map your data

Map datapills

Map datapills

The output of this action is a single document we can map to the Webinar Questions field in Salesforce:

data mapping message template

Map document to “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!

Results of message template

Results of message template in SalesForce

 

Was this post useful?

Get the best of Workato straight to your inbox.