Conditional logic

Conditional logic is a powerful feature of the Tray.io workflow editor. It can be used to ask key questions whose answers will then determine what path your workflow will take.

Tray has two key conditional logic connectors:

  • The Boolean connector can ask simple true/false questions such as 'did the new customer opt-in to marketing?' or 'is it more than 3 months since we last contacted this person?'

  • The Branch connector can be used to set multiple pathways depending on multiple different outcomes being picked up from your data. For example you may receive support tickets of 3 or 4 different types and want to set paths for each one to notify the relevant people to take appropriate action.

The Boolean connector
Copy

The following example shows using Recurly (a subscription billing management tool) to find inactive accounts.

This loops through each individual account and the boolean step checks if its state is 'inactive':

The Branch connector
Copy

Please be aware that the Branch connector is not a parallel processing connector!

If you want to set up asynchronous parallel processing to increase workflow efficiency, please see the guide to using callable workflows in our 'Processing data and scaling' section

The branch connector is essentially a 'switch statement' generator.

So it allows you to take actions based on multiple possible conditions.

To illustrate we can look at the following payload:

1
{
2
"event_id": "01HBJB40Y2RX8JFQ9ZTR36QARJ",
3
"event_type": "form_response",
4
"form_response": {
5
"answers": [
6
{
7
"type": "choice",
8
"choice": {
9
"id": "iw6D4ljxDd7G",
10
"label": "Very Satisfied",
11
"ref": "eb3d389e-9838-4060-92da-26eb6c894f38"
12
},
13
"field": {
14
"id": "lsaGBN45qcBn",
15
"type": "multiple_choice",
16
"ref": "47969b6b-7dd7-4f7f-99fb-d39d99682df2"
17
}
18
},
19
{
20
"type": "choice",
21
"choice": {
22
"id": "QLvsfypbiSgr",
23
"label": "Construction",
24
"ref": "ff85f603-202e-431b-92e5-5a7af03f5bea"
25
},
26
"field": {
27
"id": "e4cxUxSqos5p",
28
"type": "dropdown",
29
"ref": "ac185360-3406-4d55-af72-f24f132f489b"
30
}
31
},
32
{
33
"type": "choice",
34
"choice": {
35
"id": "2u87p0hxKDu9",
36
"label": "6 months to 1 year",
37
"ref": "d27a0951-9510-43c5-bcc5-54849d1a16f1"
38
},
39
"field": {
40
"id": "rFIJoo1k7Wly",
41
"type": "multiple_choice",
42
"ref": "e7606153-14fa-4952-aed2-ccea78d603dd"
43
}
44
}
45
}
46
]
47
}
48
}

If we want to extract these answers into a simple list such as:

1
[
2
"Very Satisfied",
3
"Construction",
4
"6 months to 1 year"
5
]

Then we can do this using a script connector with a switch statement:

1
exports.step = function(input, fileInput) {
2
function transformJSON(inputJSON) {
3
let outputJSON = [];
4
let formResponse = inputJSON.form_response;
5
let answers = formResponse.answers;
6
answers.forEach(answer => {
7
switch (answer.type) {
8
case "number":
9
outputJSON.push(answer.number);
10
break;
11
case "boolean":
12
outputJSON.push(answer.boolean);
13
break;
14
case "choice":
15
outputJSON.push(answer.choice.label);
16
break;
17
case "email":
18
outputJSON.push(answer.email);
19
break;
20
case "text":
21
outputJSON.push(answer.text);
22
break;
23
// Add more cases for other types if needed
24
default:
25
// Handle the default case if necessary
26
break;
27
}
28
});
29
return outputJSON;
30
}
31
return transformJSON(input.inputJSON);
32
};

Or we could loop through the answers and use the Branch connector to replicate a switch statement.

Each time we 'push' the answer using the Data storage 'append to list' operation.

After all answers have been looped through, we can then use the Data storage 'get value' operation to retrieve the final list: