Can't get that Discovery pattern to do what you need to do? Sometimes the OOB no-code UI just isn't enough. Sometimes you need more power. ServiceNow agrees and has a sparsely-documented way to invoke Javascript from within a Discovery pattern step. You can invoke Javascript to return the value for any Value field that appears on a pattern step UI form. At last count, ServiceNow has about 400 such pattern steps that invoke Javascript.
Here's a simple example: In a Value field, begin typing EVAL(. Yes, with the open paren. This will cause the pencil icon to suddenly appear to the right of the field. Click the pencil to open the script editor.
This will pop up a window which lets you craft your javascript. Here’s an example.
There’s a couple of things to note:
Here's an OOB example demonstrating that we can invoke javascript on anyValue field.
There are a few things to note about Javascript here.
You can write to the Discovery Log. Then, instantiate the Object and call its debugex function.
var logger=Packages.com.snc.sw.log.DiscoLog.getLogger('Some Identifier Here'); logger.debugex('Test display in log via EVAL script in pattern.');
Your messages show up in the pattern log for that step.
The "Some Identifier here" does not appear to show up anywhere. Also, there is an "error()" function, but it seems to behave exactly like the debugex() function.
An object called CTX allows you to get/set pattern variables and run shell commands on the target host. See Using Javascript for accessing advanced operations in Pattern Designer for more. Some function examples follow.
We go further and faster when we collaborate. Geek out with our team of engineers on our learnings, insights, and best practices to unlock maximum value and begin your business transformation today.
Transforming IT with automation, insight, and scale with ServiceOps
Streamlining IT operations and user onboarding through ServiceNow automation
Discover AI-Driven ServiceOps: Boost Efficiency & Automate Workflows