Skip to main content

Flow control

AutomatorScript supports the standard JavaScript flow-control constructs: if / else, switch / case, and the for, for...of, while, and do...while loops, along with break and continue. They behave exactly as in standard JavaScript, so for the full details we defer to MDN:

for...in vs for...of

There is one exception to the standard behavior. In standard JavaScript, for...of is used to iterate over a list of values, whereas for...in is used to iterate over the properties of an object. This is a common source of confusion, and in AutomatorScript, the for...in loop actually behaves the same as for...of.

If you need to iterate over the properties of an object, use Object.keys or Object.entries instead; for example:

for (const key of Object.keys(obj)) {
const value = obj[key];
// ...
}

for (const [key, value] of Object.entries(obj)) {
// ...
}
warning

Do not use for...in loops in AutomatorScript. As explained above:

  • If you need to iterate over a list of values, use for...of instead.
  • If you need to iterate over the properties of an object, use Object.keys or Object.entries instead.

No async/await

Integration logic is full of interaction with external systems: fetch a record from an external API, write an update to another system, read and store values in a database, send an email.

In standard JavaScript, you typically have to use async and await or promises to make this work:

// Standard JavaScript — NOT how AutomatorScript works
const response = await fetch('https://api.example.com/data');
response.json().then(data => {
log(data.name);
});

However, in AutomatorScript, you don't have to. In AutomatorScript, all functions for interacting with external systems behave as if they are synchronous. For example, the above example looks like this:

// AutomatorScript
let data = rest_get('/data', 'example-account');
log('name', data.name);

There are no async or await keywords and you don't need to use promises. Instead, the automator engine takes care of the waiting for you: under the hood it suspends execution of your package while the external call is in flight, and resumes it once the response arrives.

From the perspective of your code, everything happens in order, top to bottom. This mental model makes the code easier to read and understand.

Two related features build on this:

  • Auto-fetch lets you walk the Xurrent data model with dot notation (request.workflow.manager.name) and fetches each record on access.
  • Auto-retry transparently retries failed API calls for transient errors and rate limits.