Can ServiceNow Script Includes Use the "current" Variable?

Every once in a while, I look into the top search terms people have entered into Google or Bing (lol, who uses Bing?) that take them to this site. I do this to see if there are any common questions that people come here looking for answers to, but which I don’t yet have an article to answer. This month, that search term is “Can Script Includes use current”. This article aims to provide an answer to that question.

The question

I interpret the question “Can Script Includes use current” to mean:

If I write a function inside a Script Include (SI), and then call it from a Business Rule, can that function (in the SI) access the current object (from the Business Rule)?

Here’s an example SI which is attempting to do exactly that:

var CanIUseCurrent = Class.create();
CanIUseCurrent.prototype = {
    initialize: function() {},
    
    tryUsingCurrent: function() {
        gs.info('The current record\'s display value is: .', current.getDisplayValue());
        //^ This is bad!
    },
    
    type: 'CanIUseCurrent'
};

As you can see, the tryUsingCurrent() method is accessing a variable called current which is not declared or defined anywhere within the Script Include itself. Below, we’ll discuss whether this is possible, and whether it’s a good idea.

The short answer

The short answer to this question is: Yes, technically you can access anything that lives in the “calling” scope - but you shouldn’t. At least not without some extra steps to achieve “function purity”, which I’ll describe more in the long answer below.

If you take one thing from this article, let it be this:

Whenever possible, do not rely on “context” or the external/parent scope from which you expect your function to be called.
Never assume anything that you don’t have to, about any scope other than the function-scope you’re currently writing in.

Click below to read more, and see the full answer, including several important technical caveats!

Read more

Handling 'text/plain' and Other Unsupported Content Types in ServiceNow Scripted REST APIs

If you’ve ever tried to set up an AWS/SNS integration, you know that ServiceNow fails (rather spectacularly) to handle certain standard REST API request “Content-Types”; most notably, text/plain.

In this article, we’re going to discuss what happens when you you try to integrate with something that’s trying to send a plain-text payload, and how you can make it work. At the bottom of the article, we’ll have a simple Script Include that you can import into your instance to get around this issue, and make your plain-text integrations work.

To illustrate the issue, I’ll wake up my PDI, and set up a Scripted REST API (SRAPI) that’s meant to accept a plain-text payload. I’ll include the steps to set this up, in case you want to follow along in your own PDI.

First, in the Application Navigator, I’m going to open up…

Read more

Understanding Attachments in ServiceNow

This article was originally written in February 2016, but was last updated on 4/21/19.

Attachments in ServiceNow are not as straight-forward as email attachments, and it's not always obvious how to do what you want with them.

Recently, I needed to copy some attachments programmatically and otherwise fiddle around with attachments. After finding nothing in the ServiceNow product documentation, and very little through the usual search channels, I figured it was time to write an article about how to programmatically deal with attachments in ServiceNow. 

In this article, we'll learn how attachments really work in ServiceNow, how we can manipulate, copy, and even generate hashes for them (sometimes)! 

Read more

Using Custom Search Engines in Chrome to Quickly Navigate ServiceNow

How often have you wanted to do something like look up a record in a table by sys_id, look up a user by user_name, or jump straight to a table in ServiceNow just so you can run a query on it, but found yourself having to wait for the entire table to load, or having to navigate through several pages, waiting for each to load before you can get to the next?
For me, the answer is “multiple times, every single day”.

To that end, I’ve made this video on how to use custom Google Chrome search engines to make this process incredibly simple! Below the video, you’ll find some examples of useful “custom search engines” I’ve used in Chrome.

Click “Read more” to see the video, and some examples of how you can use this functionality!

Read more

Set Catalog Variables from URL Params (Free tool)

I've had this question come up over and over again in my career: "How do we populate variables on a catalog item or record producer, from the URL?" 

There are a couple of ways to do this, but often you'll see one-off solutions that require custom code for each and every catalog item, which also means that you have to know in advance, when designing the catalog item, what variables might be specified by URL, and how to populate them. 

I got tired of re-engineering the same basic solution over and over again, so I built a generalized tool. This tool adds to your system, a Variable Set. This variable set contains a catalog client script, but no actual variables (this is expected). The variable set is called “Parse sysparm_variables URI param”.

Once you’ve added the variable set to a given catalog item, you can populate any one or more variables in that catalog item, simply by manipulating the URL. Read on to learn more, or click here to jump straight to the tool/download page!

Read more

Outlook for Android Breaks Email Approvals (+Solution)

If you’re an Android user who uses Outlook (and who among us corporate tools is not blighted by that app on a daily basis?) you may have noticed that the Outlook app for Android has recently undergone something of a redesign recently.
I haven’t decided if I love or hate the new design personally, but however you may feel about it, it seems that they’ve done a bit more than just change the aesthetic.

That’s right, despite the fact that it’s been an IETF standard since July 1998 (see: RFC 2368), Microsoft just plum-forgot about one of the most basic functions of an email application - correctly processing mailto: links - and have now verified that this is a confirmed “feature limitation”. They “might consider adding this feature” in future updates, but for now, well, who needs standards, anyway?

Now hold on, because I can hear you saying:
“But Tim, you big dumb jerk with great hair, this is a ServiceNow development blog! Surely ranting about Microsoft is a bit silly here, isn’t it?”
And normally, yes, this would be merely a petty rant - one of my favorite kinds of rants -but in this case, the implications of this are important for us ServiceNow nerds.

Thanks to Nick Simonelli (ServiceNow developer at Chipotle - lucky jerk) for bringing this to my attention in the ServiceNow Slack community!

Read on to learn more (and see the solution)!

Read more

GlideFilter is Broken - Free Tool: “BetterGlideFilter”

Once upon a time, there was a global, instantiatable “GlideFilter” object. It was a little ugly and unintuitive, but it technically worked; usually. Nowadays though, the GlideFilter object is a scoped-API object (also available to the global scope) that is not instantiatable, and which has… some problems.
ServiceNow is aware of these problems, but - although they continue to use GlideFilter in their scoped apps (such as for vulnerability grouping rules in the Vulnerability application, which have been broken by the use of GlideFilter as of London - PRB1329737) - they have refused to fix the issues (PRB605673).

“What problems”, you ask? - Read on, to find out, and to see the solution/free tool!

Read more

Making Update Sets Smarter - Free Tool

Update Sets are how you track and promote changes to your ServiceNow environment from one instance to another (for example, from Dev to Test, and then from Test to Prod). They’re a great tool for keeping things orderly, but they don’t always tie up into a neat little bow when you need them to - especially ever since the implementation of Application Scope, on which you can see my angry ramblings here.

If you work with scoped apps (such as the HR or Vulnerability apps, or a myriad of others), you’ve almost certainly run into custom development being done in a scoped Update Set, but somehow containing Global (or non-scoped, which are basically the same as Global) records, and you’ve undoubtedly seen the dreaded error message, telling you that you can’t deploy an Update Set because it has updates from multiple scopes.

Or maybe you’ve built a Workflow in dev, checked it out, made some changes, published it, checked it out again, made some more changes, and then promoted it… but then - D’OH! - you realize you forgot to check the workflow back in before promoting your Update Set!

Or maybe you’re in an environment that has a few super green ServiceNow developers, or at a company that’s extremely liberal with granting the admin role, and you’ve had to clean up the mess after someone else renames or deletes the “Default” Update Set in Global or some other scope.

In my years as a ServiceNow admin, developer, and architect, I’ve seen all of those issues, and in each case, I’ve written a little script to save a little sanity, and make my life just a little bit easier by preventing these issues; and in this article, I’m going to share them with you.

Click below to read on!

Read more