You can create a script using the Scripts option if you want to customize the data that flows through your integrations. This article focuses on creating a script that includes a branching function. You can write the function using the Branching function stub provided in integrator.io.
The branching function in the script will allow you to separate data/records so that they flow to different branches for further processing. You can filter and perform logic on the data coming from your export before it moves on to the rest of your flow. Branching is available for destination apps and lookups.
Script - general details
For a script, you must first provide a meaningful name and description based on its purpose or function.
Script content - use function stub
After you’ve named your script, you must include or insert a function stub. The function stub makes it easy for you to write and check a function. In this case, insert the Branching function stub and then start writing your function.
Branching function stub
The Branching function stub includes
- comments for guidance (1)
- an example function name, an argument, and a return value (2)
Comments
Carefully read through the comments provided in the Branching function stub.
Function
You can use the example function name or provide another name based on the purpose of your branching. The function keyword is always provided before the name.
Argument
An options argument is passed to the Branching function. It has only the following field:
Field name | Description |
---|---|
record | A record is data, which can be in the form of an object or array. |
Return value
The function returns an integer representing a branch index or an array of integers representing the branch indices that should process the record. If an exception is thrown, the function returns an error.
Branching function - examples
Based on the function stub, here are branching function examples:
Example 1 (First matching branch): This example runs on a set of JSON data that returns customer details in a CSV file. Here the branching function matches a record based on the CustomerType value and allows it to flow through the first matching branch. The return value indicates the index of the first branch that the record will pass through.
/*Example 1*/ /*Data source - object*/ function branching (options) { if (options.record.CustomerType === 'Tier1') {return [0]} else if (options.record.CustomerType === 'Tier2') {return [1]} else if (options.record.CustomerType === 'Tier3') {return [2]} else {return [3]} }
/*Example 1*/
/*Data source- array*/ /*In record[0],[0] refers to the branch index.
You must provide the branch index for arrays. It typically starts with 0*/ function branching (options) { if (options.record[0].CustomerType === 'Tier1') {return [0]} else if (options.record[1].CustomerType === 'Tier2') {return [1]} else if (options.record[2].CustomerType === 'Tier3') {return [2]} else {return [3]} }
Example 2 (All matching branches): This example runs on a set of JSON data that returns product details in a CSV file. Here the branching function matches a record based on the Name value and allows it to flow through all matching branches. The return value indicates the indices of all the branches that the record will pass through.
/*Example 2*/ /*Data source - object*/ function branching (options) { if (options.record.Name === 'Product A') {return [0,1,2]} else if (options.record.Name === 'Product B') {return [1,2]} else if (options.record.Name === 'Product C') {return [2]} else {return [3]} }
/*Example 2*/
/*Data source - array*/ /*In record[0],[0] refers to the branch index.
You must provide the branch index for arrays. It typically starts with 0*/ function branching (options) { if (options.record[0].Name === 'Product A') {return [0,1,2]} else if (options.record[0].Name === 'Product B') {return [1,2]} else if (options.record[0].Name === 'Product C') {return [2]} else {return [3]} }
Create a script for branching
Before you begin, you must sign in to integrator.io and check if you can see the Scripts option (Resources > Scripts) in the left navigation pane.
- In the left navigation pane, expand the Resources section.
- Click Scripts.
Scripts option
- At the top right, click + Create script.
Create your script
- Enter a name and a description for your script. Give your script a name that will make it easily recognizable to others working on your integration.
Script details
- Select the Branching function stub.
Branching function stub
- Write the branching function based on the comments provided in the Branching function stub. You can do it in the Edit content section; or expand it for the full Advanced field editor. For example, as shown below.
Branching function example
- Click Save.
Note: After you create the script, you can include it when you add branching in your flow.
Comments
Please sign in to leave a comment.