Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 39 ยท Workflows and Flow Designer

Subflows, Custom Actions and Error Handling

Build reusable ServiceNow subflows and custom actions with inputs and outputs, call them from flows, and handle errors with error handlers and the executions view.

Copy-pasting steps between flows is the quickest way to create a maintenance problem. When the rule changes you have to find every copy. ServiceNow lets you package a routine once and call it from many places. There are two packages: the subflow, made of flow steps, and the custom action, made of inputs, an optional script step and outputs. This lesson shows both and how to make failures visible.

Subflows

A subflow looks like a flow without a trigger. Instead it has Inputs and Outputs.

To create one: in Flow Designer choose New > Subflow, give it a name (for example Notify on-call team), define inputs (such as Incident of type Record, table Incident, and Message of type String), build steps, and define outputs (such as Notified of type True/False). Publish it, and it appears in the action picker under Subflows of any flow.

Inputs behave like parameters in a function and outputs like return values. The caller passes pills into the inputs and can use the outputs as new pills afterward.

flowchart LR
    F1["Flow: P1 incident"] --> S["Subflow: Notify on-call team"]
    F2["Flow: Major change approved"] --> S
    F3["Flow: Security alert"] --> S
    S --> O["Output: Notified = true"]

One routine, three callers. A change to the subflow improves all three.

When to make a subflow

  • The same three or more steps appear in two flows.
  • A flow has become too long to read on one screen.
  • A routine needs its own test, with its own inputs.

Do not make a subflow for a single step; it adds a layer for no gain.

Versioning

Published subflows are called by their current version. Editing creates a new draft, and publishing swaps the version the callers use. Plan changes to inputs carefully: adding a mandatory input will break callers that do not supply it.

Custom actions

The built-in action library covers many steps (Create Record, Send Email, Ask For Approval, Log). When you need a step that does not exist, you build an action. In Flow Designer choose New > Action (available through Action Designer).

An action has:

  1. Inputs, with names and types.
  2. Steps, such as a Script step, a REST step or a built-in action. The script step reads inputs through the inputs object and writes results to the outputs object.
  3. Outputs, which later steps see as pills.

A script step has this shape (read-only code; the Rhino-based server JavaScript runs on the instance, not on this site):

JavaScript
(function execute(inputs, outputs) {
  var gr = new GlideRecord('sys_user');
  gr.addQuery('email', inputs.email);
  gr.query();
  outputs.found = gr.next();
  outputs.sys_id = gr.found ? gr.getUniqueValue() : '';
})(inputs, outputs);

The Scripting chapter explains GlideRecord. Here, notice the contract: everything in, everything out, nothing else. An action should not depend on hidden state, so that it can be tested alone from the Action Designer's Test button.

Actions vs subflows

Question Subflow Custom action
Built from Flow steps Script, REST or other steps
Best for Business routines One technical operation
Owned by Process owner Developer or advanced admin
Can have a loop of steps Yes Via script

Error handling

Real automations fail: an email address is empty, a REST call times out, a lookup returns nothing. Flow Designer gives you three tools.

1. Error handler. Most actions let you turn on error handling. If the action fails, the flow runs your handler steps instead of stopping, for example log the problem and create a task for support. Build the handler so that it never fails itself.

2. Conditions after risky steps. Look at the action's output. Many actions return a status or an error message pill. Add an If that checks it and branches.

3. The execution view. Every failed step is marked with the error message and the inputs it received. Start there before you change anything.

Also decide the flow's error behavior explicitly: stop with an error, continue, or compensate (undo earlier steps). The platform does not undo changes automatically. If step 2 created a record and step 3 failed, the record stays.

Model a handler

The JavaScript below is a stand-in that shows how a handler changes the outcome of a failing step. Nothing here is ServiceNow code.

JS Stand-in: step with and without an error handler

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Without a handler, the record update never happens. With one, the failure is recorded and the flow keeps going. Choose carefully: sometimes stopping is right, because a later step would be wrong without the earlier one.

A subflow as a function

A subflow behaves like a function with typed inputs and outputs. The stand-in below makes that analogy concrete, including a check that rejects a missing required input, which is the mistake that breaks callers after a subflow changes.

JS Stand-in: a subflow with required inputs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Maintenance tips

  • Document each subflow's purpose in its description and give inputs help text.
  • Keep script steps short; put long logic into a Script Include (covered in the Scripting chapter) and call it.
  • Search for where a subflow is used before you change its inputs.
  • Test actions on their own before wiring them into flows.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do a subflow's inputs and outputs resemble?

    Callers pass values into inputs and read outputs afterward, like calling a function.
  2. 2. When is a custom action the better choice than a subflow?

    Actions package one operation with inputs, a script or REST step and outputs.
  3. 3. A step fails and no error handler is set. What happens by default?

    Unhandled errors stop the flow, and changes already made stay in place.
  4. 4. You add a new mandatory input to a published subflow. What is the likely effect?

    Plan interface changes carefully because existing callers may not supply new mandatory inputs.

Summary

  • Subflows package reusable flow steps with inputs and outputs; custom actions package one technical operation.
  • Build subflows when a routine repeats or a flow grows too long.
  • Use error handlers, checks after risky steps and the executions view to make failures visible.
  • Changing a published interface can break callers, so search usage first.

Next: Approvals, Notifications and SLAs