Articles in this section

Create a branching script

A branching script contains a function that decides which branch each record flows through. You use it when rules can't express your branching logic. This article shows you how to create a script with the Branching function stub, and describes the function's argument, return value, and examples. Branching is available at import and lookup steps.

Prerequisites: Sign in to integrator.io and confirm that Scripts appears under Resources Resources icon in the left navigation.

Create the script

  1. Go to Resources Resources icon, and then select Scripts.
  2. Select + Create script.
  3. Enter a name and description. Use a name that others working on your integration can recognize.
  4. In Insert function stub, select Branching.
  5. Write the branching function, following the comments in the stub. Write it in Edit content, or select Expand window to work in a larger editor.
  6. Select Save.

To use the script, see Configure branch conditions.

Branching function stub

The Branching function stub contains guidance comments, and an example function name, argument, and return value.

Function

The stub includes a function name that you can keep or replace with a name that reflects your branching. The function keyword always comes before the name.

Argument

The function receives one argument, options, which has these fields:

  • record — The data for the record. An object or an array, depending on the data source.
  • settings — All custom settings in scope for the router that is running.
  • testMode — A Boolean that is true in test mode and in previews.
  • context — Metadata about the MCP caller: token, byUser, client, and source. Available in tool runs only.

Return value

The function returns an integer or an array of integers. Each integer is the index of a branch that processes the record. For First matching branch, return the index of the one branch the record flows through. For All matching branches, return the indexes of every branch the record flows through. If the function throws an exception, it returns an error.

Examples

First matching branch

This example routes customer records by CustomerType. Records with an unlisted type flow through branch 3.

When the record is an 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];
  }
}

When the record is an array:

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];
  }
}

All matching branches

This example routes product records by Name. A record flows through every branch whose index the function returns.

When the record is an 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];
  }
}

When the record is an array:

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];
  }
}