Advanced message editor: Liquid markup

When creating an approval template, the standard functionality allows you to select a number of ticket fields from a drop-down menu that will be sent to the approver at the time the approval is initiated. This is by far the simplest and easiest way to build an approval template trouble free.  

However, if you really need that extra complexity we have built functionality in the app to cater for liquid markup. 

There is a quick reference to what Liquid is and things you can do with it here http://shopify.github.io/liquid/ and it is also used through-out Zendesk as documented here.

Below we'll be walking through the following examples to show what is possible with Liquid.

 

Example 1: Optional fields - only sending them to the approver if they're filled

Example 2: Calculating values from numeric fields

Example 3: Making optional fields required based on the input of other fields

  


 

Example 1: Optional fields

For our first example, let's pretend we have many ticket fields on a form and we only want the approver to be sent information in some ticket fields if something has been entered. No problems... you can define a condition using liquid markup to say, if the field is empty it is not required, nor is the field included in what is sent to the approver. If the field has been filled in, then vice-versa, it will be included it in the message that gets sent to the approver. 

So for a "Purchasing Request" form that uses the following ticket fields: 

  • Item
  • Item description
  • Purchase size
  • Reason for purchase
  • Date required by
  • Unit price
  • Quantity
  • Vendor name
  • Vendor contact

Many of these fields might be optional. So we don't want to clog up the email that the approver sees by listing out ticket fields that haven't been filled. 

The way we can do this is by adding an 'if statement' around optional fields to say they're not required if they're not filled in. Let's start with the "Item description" field - the if statement would look something like this:

{% if ticket.custom_field_360000279108 %}Item description: {{ticket.custom_field_360000279108}}
{% endif %}

(Learn more about how this if statement works here )

To see this statement in context lets first have a look at what the fields look like when switching from the default settings...

basic_code.gif

After adding the if statement above to the code, the message will end up looking like this:

SweetHawk__-_Agent.png

Note the positioning of the if statement. The reason for this is that carriage returns in the liquid are currently transferred to the UI (what the agent and approver see) so you need to make sure there are the right amount of new lines per number of items otherwise you may end up with unwanted white-space. 

Finally, over on the ticket, we can see how this works in action:

description.gif

 

You can now repeat this if statement for all of the other fields you need to make optional in the approval.

 


 

Example 2: Calculating values

For our next example, maybe we want to make a calculation from the existing fields that have been filled in. So in the case of the purchasing request maybe you want to calculate the total cost by multiplying the 'unit price' and the 'quantity' ticket fields. 

You can find the liquid markup instructions for multiplication here: http://shopify.github.io/liquid/filters/times/ 

So if we apply this concept using the ticket field placeholders it would result in adding an additional like to the message which would be:

Total cost: ${{ticket.custom_field_80500407 | times: ticket.custom_field_80887708 }}

This will result is a field that automatically calculates the total cost and would look something like this:

total_cost.gif

 


 

Example 3: Making optional fields required based on the input of other fields

Ok, so one possibility is that you need to make ticket fields required based on a selection from another field.

For example. Continuing with our 'Purchasing' use case, let's say that if from the "Purchase size" drop-down field the agent was to select "Large (more than $5000)" they were then required to fill in the ticket field "Reason for the purchase" before they're allowed to send off the approval.

BUT, let's say you also want the "Reason for the purchase" field to be optional as well (as per what was demonstrated in example 1) so that if something other than"Large (more than $5000)" is selected from "Purchase size" the information will still be sent to the approver.

For this we'll need to start with the optional if statement like this...

{% if ticket.custom_field_80887728 %}Reason for Purchase: {{ticket.custom_field_80887728}}
{% endif %}

Same as before we're saying that if a reason has been entered into the field, then include it in what's sent. Cool.

Now we need to add in the extra condition to say, alternatively, if the purchase size is a large one, the reason for purchase field is now mandatory. So all we do is squeeze in another if statement by adding an 'else' like this:

{% if ticket.custom_field_80887728 %}Reason for Purchase: {{ticket.custom_field_80887728}}
  {% else %}
  {% if ticket.ticket_field_option_title_360000275267 == "Large (more than $5000)" %} Reason for Purchase: {{ticket.custom_field_80887728}} 
  {% endif %}
{% endif %}

When putting this code into practice, once again we have to be mindful of where the carriage returns go so as not to result in unwanted white space and also not result in lack of carriage returns where they should be.

Ok so from the same default code as in example 1, here's what it looks like when we add just the code above into the approval template:

SweetHawk__-_Agent.png

And in practice this here's how it works:

reason.gif

 

If you want to add other selections from the "Purchase size" drop-down menu, you can use "or" to add extra statements in the if statement (as per this documentation).

Here's what it looks like:

{% if ticket.custom_field_80887728 %}Reason for Purchase: {{ticket.custom_field_80887728}}
  {% else %}
  {% if ticket.ticket_field_option_title_360000275267 == "Large (more than $5000)" or ticket.ticket_field_option_title_360000275267 == "Medium ($1500 - $5000)"%} Reason for Purchase: {{ticket.custom_field_80887728}} 
  {% endif %}
{% endif %}

  

Ok sweet! So there are a few examples of how to build conditions or calculations with liquid markup. If you have any questions just post them in the comments below :-) 

 

Further reading: Learn how to use liquid markup to send ticket attachments to approvers here. 

 

 

 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk