Open Navigation

Useful Scripts

This section of the documentation is dedicated to easy-to-implement scripts that can add power to your workflows and make simple tasks easier.

These will make use of the Script Connector or Lodash Helper.

These are early days for this section, but see below for some useful scripts to get started!

Removing duplicate objects from an array (Lodash)

A limitation of the List Helpers remove duplicates function is that it can only remove simple items from an array, and it can't remove objects based on a particular field in that object being unique.

For example you may have just fetched an array of message objects from Data Storage (you might have created this array by setting the data with the 'Append to list' operation and then used the 'Get value' function to call your message list), which you want to make unique by their ticket_url:

{
"value": [
{
"attachment_text": "Support: hello there! I need some help syncing records",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1537",
"ticket_text": "Ticket 1537 from ronburgundy@news.com"
},
{
"attachment_text": "Copied from original request - 401 unathorized",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1459",
"ticket_text": "Ticket 1459 from klaus.kinsky@kinsky.com"
},
{
"attachment_text": "Hi There, I can't access my stuff",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1536",
"ticket_text": "Ticket 1536 from gretchen.mol@mol-shopping.com"
},
{
"attachment_text": "Hi Team - we need to process multiple records",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1564",
"ticket_text": "Ticket 1564 from roger@ramjet.com"
},
{
"attachment_text": "Roger here, following up on my previous message",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1564",
"ticket_text": "Ticket 1564 from roger@ramjet.com"
},
{
"attachment_text": "Help, I'm a bit stuck",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1556",
"ticket_text": "Ticket 1556 from penelope@pitstop.com"
},
{
"attachment_text": "It all seems to be lost",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1536",
"ticket_text": "Ticket 1536 from gretchen.mol@mol-shopping.com"
},
{
"attachment_text": "Hi, I need to process lots of data",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1173",
"ticket_text": "Ticket 1173 from sophia@copolla.com"
},
{
"attachment_text": "Hello, we need to troubleshoot some problems",
"ticket_url": "https://mycompany.zendesk.com/agent/tickets/1591",
"ticket_text": "Ticket 1591 from pat@pending.com"
}
]
}

To do this we can use the uniqBy method with the Lodash Helper.

The Lodash uniqBy method requires 2 parameters:

  1. The array
  2. The field which should be unique

So if we have the above array of messages being returned by the storage-2 connector in our workflow we can use the following setup of the Lodash Helper to remove objects which contain the same ticket_url:

lodash-uniqby-params-in-props

This will then remove tickets 1564 and 1536 from the above list of messages.

Checking if a piece of text contains multiple words or phrases (Script connector)

The Text Helpers 'contains' operation is very useful for checking if a piece of text contains a keyword or phrase. It is however limited to only one word or phrase.

You can extend this by using the match javascript function which can use regex functionality.

An example scenario might be that we are looping through a list of results and each result has an array of 'attachments', with the first [0] attachment containing text. We might want to search through the text attached to each result.

We would do this by adding a script step and setting a variable such as 'text' which uses a jsonpath like $.steps.loop-1.value.attachments[0].text to pull in the text from the result.

This can then be used in the script box where we can add a list of keywords separated by |:

exports.step = function(input) {
const text = input.text
const res = text.match(/(csv|sheets|boolean|webhook|sso|loop|call workflow|callable|script|object|debug|text helper|data storage|list|http client)/gi);
return res;
};

This will return results such as:

script-contains-multiple-keywords

Or:

script-contains-multiple-keywords-second-result

As you can see this script returns a list of all instances of the keywords found.

For the next step in your workflow you could either extract and count these keywords or, more likely, you could use a boolean connector to check that the result is not null:

script-contains-multiple-keywords-boolean

Was this article helpful?
Yes
No