42 Ways to Access Workflow Variables

It's not always easy to discover when and how to access orchestration and workflow input and activity output values when designing a workflow in ServiceNow. 

Ok, forty-two ways might be a slight exaggeration. We'll take a look at a couple shortcuts, some frustrating limitations, and an easy consistent solution to speed up your workflow development.

 

The Setup

Let's start with a custom activity that has output variables. This "run script" activity will contribute these variables to all downstream activities.

Next we will create a workflow with some workflow inputs to make things a little more interesting. These variables will be accessible throughout the entire workflow.

 

Time to add our custom activity to the new workflow.

Shortcuts

All downstream activities from "Get Flight Briefing"  should be able to access all value outputs from this activity as well as all of the workflow input values.

One of the few places this is really easy to accomplish is the REST Message activity. Once the activity is dropped on the canvas we can view all workflow data bus values from the right-pane "Data" tab. All of these values can be mapped to an appropriate field in the REST message form. In this example, we are using both workflow inputs and previous activity output values to define REST message variable replacements. The variable path is automatically generated for us.

The path for workflow variables is pretty easy if you remember the variable names. Activity output variables can be a bit tricky as they are defined by the activity number assigned to the activity for the workflow. These are defined when you drop an activity onto the canvas and will not change. However, if you delete an activity from the workflow and re-add it, the activity will have a different id.

 

Partial Shortcut

As you will find, this shortcut is not always available but there's one more option we can rely on. An example is the "Run Script" activity. It allows us to access activity output variables by expanding the script object browser on the script field. Simply click on an activity output name to insert the path to that value into the script.

You may have noticed that since this is a script, we don't need to wrap the variable path in ${} as we do when we want to resolve the value from non-script text inputs.

 

Limitations

For many activities neither of these shortcuts are available. You'll find this frustrating when you want to use workflow inputs and activity outputs as values in a "Set Values" activity, which seems like a pretty common case. Drag support from the Data tab is not currently supported. You'll need to know the variable replacement path as we saw above (${data.get(3).notams}).

 

The CAVU solution

You've scrolled this far so I'll reward you with the shortcut I discovered to eliminate this frustration. I figured that if drag support is enabled in some locations, then the data bus DOM elements should have an attribute with the path that is used when dragging is supported.

So...I decided to inspect the elements and it turns out they contain everything we need. Using your browser dev tools, inspect the element of one of the data variables. The replacement variable values that we need are stored in the element's "databus_id" attribute.

Now that you have easy access to the data bus variable paths at all times, you just need to know when to use replacement syntax ${}, and when to use standard variable syntax data.get(i).var_name. The general rule is if it's in a script block, use standard syntax, all other cases use the replacement syntax. Also note that just because you know the variable path, that doesn't mean that the values are resolvable from everywhere, so be sure to test. I haven't found many cases where I needed a value and couldn't get access to them.

It would be really nice if ServiceNow either added full and consistent support through all activities or at least display the databus_id attribute in the element title for easy hover-over viewing. Until then, at least you know a shortcut.