Articles in this section

Create a branching script (using the function stub)

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.

Developer mode

To use this feature now, you must enable Developer mode. Developer mode will be deprecated after May 21, 2025. This change will not affect new or existing integrations, flows, scripts, stacks, tokens, integration apps, or APIs.

Script - general details

For a script, you must first provide a meaningful name and description based on its purpose or function.

script_general.png

Script details

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.

script-content-1.png

Script your content with the branching function stub

Branching function stub

The Branching function stub includes

  • comments for guidance (1)

  • an example function name, an argument, and a return value (2)

script_content_stub.png

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.

  1. In the left navigation pane, expand the Resources section.

  2. Click Scripts.

    resources-scripts.gif

    Scripts option

  3. At the top right, click + Create script.

    create-script.gif

    Create your script

  4. 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-name-description.gif

    Script details

  5. Select the Branching function stub.

    insert-branching_stub.gif

    Branching function stub

  6. 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.

    branch_stub_example.png

    Branching function example

  7. Click Save.

    Note

    After you create the script, you can include it when you add branching in your flow.