Datadog

Datadog Expertise

RapDev is a Datadog Premier Partner focused on accelerating our customers’ time to value.
600
Implementations
110
US-Based Engineers
68
Datadog Certifications

Security & Managed SOC

Quickly and seamlessly implement Cloud SIEM, ASM, SCA, and Cloud Security Posture Management to power a modern DevSecOps strategy

Incident Management

Transform data into high-confidence, actionable incidents using AI-driven detection, clear ownership models, and automated remediation

Marketplace Integrations

RapDev is proud to offer more Datadog Marketplace integrations than any other partner

ServiceNow

ServiceNow Expertise

RapDev is a ServiceNow Elite partner focused on helping you drive business outcomes with the ITx suite.
4.7
CSAT Score
136
Product Line Certs.
67k
AI Agents Discovered

Agentic AI & AI Governance

Deploy and scale production-ready agentic AI to automate workflows and accelerate ServiceNow outcomes

Enterprise Architecture

Connect your technology landscape to business strategy to optimize investments, reduce risk, and accelerate modernization

ServiceNow Store

Leverage RapDev’s certified apps and AI Agents to expedite operations on the Now Platform
Blog
Company

About RapDev

RapDev is powered by a team of experienced, U.S. based engineers focused on redefining service operations through AI, automation, and modern observability.

Join the RapDev team

Our no-frills approach to collaborating is what allows us to deliver the best. Our team is growing and we’re looking for the best in the game.

Press

Latest news and announcements from RapDev

Events & Webinars

From hands-on workshops to industry-leading conferences

Resources

Back to blog

Invoking Javascript in Pattern Steps

Can't get that Discovery pattern to do what you need to do?

X

min read

December 22, 2022

Rob Witty

More Power

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.

How to do it

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:

  • Don't do a return statement! The system returns the value of the last var that you populate. Add a return statement, and the script will fail.
  • You cannot declare and assign the returned value as the last line. That is to say, line 14 cannot be var answer = getMacAddrFromName(name). This will fail.

Here's an OOB example demonstrating that we can invoke javascript on anyValue field.

It’s Javascript, mostly.

There are a few things to note about Javascript here.

  • You can find hundreds of OOB examples to get ideas on what is possible, along with various objects that do many things for you. Query the sa_patterns table where pattern text contains javascript. Filter the list further to look for particular items of interest.
  • You can reference variables by enclosing them like ${cmdb_ci_etc_etc}.You can call script includes, so long as they exist on the Mid Server*.***
  • OOB examples make heavy use of escaping double quotes. Using single quotes often works without having to clutter the code with escape characters. If you encounter unexpected results, review ServiceNow's examples and how they deal with quotes and strings.
  • There are numerous OOB examples instantiating useful Java objects, some of which include: CommandRunner (to run commands on the target host), HttpCallHandler, DNSUtils, ArrayList, and more.

DiscoLog

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.

CTX

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.

  • CTX.getAttribute(name): This method uses the attribute name as its input and returns its value. Ex: CTX.setAttribute('sql_instances_labels', sqlLabels);
  • CTX.setAttribute(name, Object): This method sets an attribute in the context. The first argument is the attribute name. The second is the value. Ex: var response = CTX.getAttribute('sql_instances_response');
  • CTX.getDiscoveryProvider: I'm still trying to figure out what this does, but I saw it in an OOB script.
  • ommandManager.shellCommand: This method executes a command on a target host. On UNIX machines or network devices, this command uses SSH. On Windows machines, it runs commands using the 'cmd'. EX: CTX.getCommandManager().shellCommand( "hostname", false, null, null, CTX); Its arguments:Command: string containing the command to be executedsuperUser: Boolean argument defining if the command needs to be executed with elevated rights like sudoexecutionMode: put null herecommandParams: put null hereexecutionContext: put CTX here

References