Tray Platform / Standard / Best Practices / Managing Data / Manipulating data with helpers

Manipulating data with helpers

When retrieving data from one service, you may wish for it to be presented in a different way in another service. Alternatively, some services will only accept data presented in a specific way.

There are a number of reasons why you may need to manipulate data. Fortunately, there are also a number of ways to do so.

Using Core Connectors and Helpers

This example shows a simple guide of editing data from one service to be easily read in another service. We will receive data from Salesforce which contains details of a sales agent's neglected leads - whose last activity was at least two weeks ago and therefore urgently needs following up. This will be sent to the sales agent via Slack every Monday morning. We will make use of the Loop Connector, the Data Storage Connector, the Date & Time Helper and the Object Helper.

The data we receive from Salesforce will be the neglected lead's first name, last name, company, phone number and the date of the last activity. The activity date is given in the format YYYY-MM-DD so we will reformat this to MMM-DD-YYYY (eg, Jan 10th 2019), concatenate the first and last name to give a full name and then push the message to Slack.

1 - Set up the Scheduled Trigger

Set the Operation for your trigger to Every Week and to run every Monday at 9.00am:

2 - Setting the date to check against

We need to get the date two weeks prior to every time the workflow runs to use as a minimum for Salesforce to check against. Salesforce will also only accept the date presented as YYYY-MM-DD so we will also need to make sure it is in this format. We will need three Date and Time Helpers to achieve this.

With the first Helper labelled "Get Current Timestamp", set the operation to Get current timestamp.

The second Helper labelled "2 Weeks Ago", sets the time to two weeks before the current date. Set this operation to Minus from date, set the Date to the output of the Get Current Timestamp step and Weeks to 2.

The third Helper labelled "Format Date for Salesforce", converts the timestamp (currently YYYY-MM-DDTHH:mm:ssz) to what Salesforce will accept. Set the operation to Format date, the Date to the output of the 2 weeks ago step and the Format to YYYY-MM-DD.

3 - Set the Salesforce Connector

In the Salesforce connector step, authenticate with your Salesforce account and set the operation to Find records, the record type to Lead and add five fields, set to First Name, Last Name, Company, Phone and Last Activity.

This returns an array called records with a number of objects inside representing each neglected lead. These object contain the keys FirstName, LastName, Company, Phone and LastActivityDate.

4 - Set the boolean, looping the results and manipulating the returned data

We will set a boolean step to check if any neglected leads were returned and if not we will terminate the workflow as no message will need to be sent. If at least one has been returned, we will loop over each result.

While in the loop, we will do the majority of the data manipulation for each lead that comes through. First, we will use the Date & Time Helper to change the format of the date that was returned. Set the operation to Format Date, Date to the output of the LastActivityDate from the loop step and the Format to MMM DD YYYY.

We will then use two Object Helpers to change the format of the date that was returned and to add the full name to the list of results.

The first Object Helper which changes the date format will have the operation set to Add value by key. We will then set the Object to the output of the loop (which represents the details of one neglected lead). To access the date that was returned, we can set the key to be the same as it is currently in the object LastActivityDate. This will replace it's current value. Finally, we set the Value as the output of the previous Time and Date Helper.

For the next Object Helper we are going to be adding a new key-value pair, which will be the full name of the lead, to the object. The operation will be the same Add value by key but the Object this time will be the output of the previous Object Helper step to make sure we include the correctly formatted date.

Now to add the full name, we set the Key to FullName (keeping the naming convention). Ensure the value data type is set to String so we can use concatenation to join the FirstName and LastName of each lead together to create FullName. This is done by using curly braces around the JSON path of each value.

The last step in this loop will save the resulting updates to be sent to Slack later. Using a Data Storage Connector we set the operation set to Append to list to replicate the array of objects that is output from the Salesforce step. We need to ensure that the "Create if missing" is field is ticked.

We ensure Scope is set to Current Run and set the Key, in this case to neglected, so we can reference it later.

The new object looks like the below.

5 - Get manipulated leads to send message via Slack

The leads pulled from Salesforce are ready and readable for Slack. As they are still objects inside of an array, we need to loop push each one to Slack.

First, we need to get the array. Using the Data Storage connector, set the operation set to Get value, Scope to Current run and the Key set to neglected.

We then loop over this collection and within this loop, set a Slack connector.

Authenticate your account in the Slack step and set the operation to Send message. Then you can use the data in the loop to set the way you would like the message to be displayed in Slack. This examples sets the Username as NOT CONTACTED FOR 2+ WEEKS:, the icon as the Salesforce logo and the attachment colour as red. We use the company name as the title of the field and again use concatination to build the message.

Which looks like this when posted to Slack.