logo

NJP

Can ServiceNow Script Includes Use the "current" Variable?

Import · Dec 18, 2019 · article

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()); }, 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.

There are exceptions to this rule, such as for truly-global objects like _gs_ or the _GlideRecord_ class. For the most part though, if it isn’t **global**, and it isn’t **defined** or **passed** into your script, **don’t rely on it**; or, more to the point: **don’t assume that it exists**!

## The short solution

If you just want a quick answer without a lot of explanation, here it is:

The correct way to access the “_current_” object in a Script Include, is to **pass it into the function you’re calling** (and preferably, to use a variable name within the method _other_ than “_current_”).

> **_Note_**_: Keep in mind that when handling_ **_objects_** _in JavaScript, you’re passing by_ **_reference_**_, not by_ **_value_**_. This basically means that anything that you do to the object in the Script Include, will also happen to that object in the Business Rule - even if your SI method doesn’t return anything!_
> _Since this is the “short” solution, I’ll spare you the explanation of_ **_pass-by-reference_** _(PBR). If you want to know more about it - and you should - you can read more in my article on the subject,_ [**_here_**](http://pbr.snc.guru/)_._

## The longer answer

Okay, so here’s where we’re going to get into some “philosophical” shit, but I promise not to go _too far_ off the deep-end on you.
Let’s talk about **function purity**, and why it’s **awesome**.

### What is a pure function?

A _pure_ function is a function where the _results_ of the function are not dependent on anything outside of the function itself, and what’s passed into it. A pure function also has no side-effects (it takes an input, returns an output, and doesn’t modify anything outside of its own scope).
For the most part, this means that what the function _returns_ will be effectively identical as long as the exact same value is passed into it. Doesn’t matter what time of day it is, what scope it’s run in, or whether robots have risen up to finally overthrow humanity; the function will always return the exact same value, as long as the input parameters are exactly the same.
To put it another way, **pure functions do not care about any external “state” or context**.

Of course, **_truly pure_** functions are often not possible or ideal in ServiceNow, because a lot of the code we write is going to rely on what data is found in the **database**; and the database is definitely “external” to the function.
That said, you can imagine doing a query in one function, and passing that GlideRecord into another function. That second function would be able to be effectively pure, because as long as a GlideRecord corresponding to a record with the exact same values is passed into it, you can reasonably expect it to behave in the exact same way.

> **_Note_**_: This isn’t to say that a function should always do exactly the same thing every time it’s called; just that the only thing which should impact what the function does, should be what you’re passing into it (whenever possible)._

### Why are pure functions awesome?

Pure functions are much more **readable** and much more **predictable** in terms of what they’re going to _actually do_, given a specific input. This makes debugging, modifying your code’s functionality, and writing modular code much easier.

Pure functions are also often much more **reusable**, which is a desirable property of code. The DRY (_Don’t Repeat Yourself_) principle applies not only to blocks of code within a function, but to entire functions as well! By writing a function which doesn’t rely on state, you’ve written a function which can be reused from any context, to do the one specific job that it was designed to do.

As an example of this reusability, consider the _CanIUseCurrent_ Script Include near the top of this article. - What if you wanted to call this function from a background script, or from another Script Include? You’d have to declare a variable in the calling scope called “_current_” and set it to a GlideRecord on the same table as the Business Rule from which the _tryUsingCurrent()_ method was originally intended to be used.
And what if you wanted to call it from _another_ Business Rule on another table? - You’d be _forced_ to name _some other_ variable “_current_”, which would collide with the existing “_current_” variable in that Business Rule. You can imagine how messy things could get!

**Pure functions** don’t have any of these problems.

### How do I write a pure function?

Let’s have another look at the example Script Include from the top of this article:

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

In this script, you’ll see that on line 6, it’s calling _current.getDisplayValue()_. However, the _current_ object is not defined anywhere in that function, and isn’t passed into it! So how could we rewrite this function so that it’s “pure”?

Easy! Rather than relying on the state/scope/context from which your code is called, simply have your code accept the relevant “state” as input parameters! Consider the following update to the _CanIUseCurrent_ Script Include mentioned above. Note the changes to the _tryUsingCurrent()_ method, on lines 5 and 6:

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

With the **old version** of this SI, we’d have to call the function from a Business Rule with the “_current_” object containing a record that the script expects to be there. That call would look something like:

(function executeRule(current, previous ) { new CanIUseCurrent().tryUsingCurrent(); })(current, previous);

Note that I haven’t passed anything into the function being called, so it’s entirely reliant on the state of the Business Rule from which I’ve called it.
However, with the **new version** of the Script Include above (where the _tryUsingCurrent()_ method accepts an argument that it calls _grIncident_), the function becomes “pure” and can be called in pretty much the same way, with only a minor tweak - passing in _current_ as an argument to the function (now that it accepts an argument) like so:

(function executeRule(current, previous ) { new CanIUseCurrent().tryUsingCurrent(current); })(current, previous);

So there you have it! Aim for purity whenever you can by passing any necessary context into your function, and try to avoid your code relying on any external-state assumptions. 👍

* [ March 2024 ](https://snprotips.com/blog?month=03-2024)
* [ February 2024 ](https://snprotips.com/blog?month=02-2024)
* Feb 12, 2024 [5 Lessons About Programming From Richard Feynman](https://snprotips.com/blog/2024/5-lessons-about-programming-from-richard-feynman)
* [ July 2023 ](https://snprotips.com/blog?month=07-2023)
* [ May 2023 ](https://snprotips.com/blog?month=05-2023)
* [ April 2023 ](https://snprotips.com/blog?month=04-2023)
* [ December 2022 ](https://snprotips.com/blog?month=12-2022)
* Dec 13, 2022 [ServiceNow Developers: BE THE GUIDE!](https://snprotips.com/blog/2022/12/13/be-the-guide)
* [ October 2022 ](https://snprotips.com/blog?month=10-2022)
* [ August 2022 ](https://snprotips.com/blog?month=08-2022)
* [ March 2022 ](https://snprotips.com/blog?month=03-2022)
* [ February 2022 ](https://snprotips.com/blog?month=02-2022)
* [ May 2021 ](https://snprotips.com/blog?month=05-2021)
* May 3, 2021 [Adding a Guided Setup to Your ServiceNow Application](https://snprotips.com/blog/2021/4/27/adding-a-guided-setup-to-your-servicenow-application)
* [ April 2021 ](https://snprotips.com/blog?month=04-2021)
* [ February 2021 ](https://snprotips.com/blog?month=02-2021)
* [ November 2020 ](https://snprotips.com/blog?month=11-2020)
* Nov 17, 2020 [SN Guys is now part of Jahnel Group!](https://snprotips.com/blog/2020/11/17/the-sn-guys-a-servicenow-consulting-firm-acquired-by-jahnel-group-an-enterprise-software-agency)
* [ September 2020 ](https://snprotips.com/blog?month=09-2020)
* Sep 14, 2020 [Better ServiceNow Notifications (& Another FREE Tool!)](https://snprotips.com/blog/2020/8/27/eliminating-servicenow-notification-nuisance-amp-another-free-tool)
* [ July 2020 ](https://snprotips.com/blog?month=07-2020)
* Jul 31, 2020 [Debugging Client & Catalog Client Scripts in ServiceNow](https://snprotips.com/blog/2020/7/31/debugging-client-amp-catalog-client-scripts-in-servicenow)
* [ January 2020 ](https://snprotips.com/blog?month=01-2020)
* Jan 20, 2020 [Getting Help from the ServiceNow Community](https://snprotips.com/blog/2020/1/20/getting-help-from-the-servicenow-community)
* [ December 2019 ](https://snprotips.com/blog?month=12-2019)
* Dec 18, 2019 [Can ServiceNow Script Includes Use the "current" Variable?](https://snprotips.com/blog/2019/12/18/can-script-includes-use-the-current-variable)
* [ November 2019 ](https://snprotips.com/blog?month=11-2019)
* [ April 2019 ](https://snprotips.com/blog?month=04-2019)
* [ March 2019 ](https://snprotips.com/blog?month=03-2019)
* Mar 11, 2019 [GlideFilter is Broken - Free Tool: “BetterGlideFilter”](https://snprotips.com/blog/2019/3/11/glidefilter-is-broken-heres-an-alternative)
* [ February 2019 ](https://snprotips.com/blog?month=02-2019)
* Feb 27, 2019 [Making Update Sets Smarter - Free Tool](https://snprotips.com/blog/2019/2/27/making-update-sets-smarter)
* [ November 2018 ](https://snprotips.com/blog?month=11-2018)
* [ October 2018 ](https://snprotips.com/blog?month=10-2018)
* [ September 2018 ](https://snprotips.com/blog?month=09-2018)
* [ July 2018 ](https://snprotips.com/blog?month=07-2018)
* Jul 23, 2018 [Admin Duty Separation with a Single Account](https://snprotips.com/blog/2018/7/23/admin-duty-separation-with-a-single-account)
* [ June 2018 ](https://snprotips.com/blog?month=06-2018)
* [ May 2018 ](https://snprotips.com/blog?month=05-2018)
* May 29, 2018 [Learning ServiceNow: Second Edition!](https://snprotips.com/blog/2018/5/29/learning-servicenow-second-edition-is-now-available-for-pre-order)
* [ April 2018 ](https://snprotips.com/blog?month=04-2018)
* [ March 2018 ](https://snprotips.com/blog?month=03-2018)
* [ February 2018 ](https://snprotips.com/blog?month=02-2018)
* Feb 11, 2018 [We have a new book! ](https://snprotips.com/blog/2018/2/11/we-have-a-new-book)
* [ November 2017 ](https://snprotips.com/blog?month=11-2017)
* Nov 6, 2017 [Requiring Attachments (& Other Miracles) in Service Portal](https://snprotips.com/blog/2017/11/5/a-better-way-to-check-for-attachments-including-service-portal)
* [ September 2017 ](https://snprotips.com/blog?month=09-2017)
* Sep 12, 2017 [Handling TimeZones in ServiceNow (TimeZoneUtil)](https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil)
* [ July 2017 ](https://snprotips.com/blog?month=07-2017)
* [ June 2017 ](https://snprotips.com/blog?month=06-2017)
* [ May 2017 ](https://snprotips.com/blog?month=05-2017)
* May 9, 2017 [Work at Lightspeed: ServiceNow's Plan for World Domination](https://snprotips.com/blog/2017/5/9/work-at-lightspeed-servicenows-plan-for-world-domination)
* [ April 2017 ](https://snprotips.com/blog?month=04-2017)
* [ March 2017 ](https://snprotips.com/blog?month=03-2017)
* Mar 12, 2017 [reCAPTCHA in ServiceNow CMS/Service Portal](https://snprotips.com/blog/2016/10/6/implementing-recaptcha)
* [ December 2016 ](https://snprotips.com/blog?month=12-2016)
* [ November 2016 ](https://snprotips.com/blog?month=11-2016)
* Nov 10, 2016 [Chrome Extension: Load in ServiceNow Frame](https://snprotips.com/blog/2016/11/10/chrome-extension-load-in-servicenow-frame)
* [ September 2016 ](https://snprotips.com/blog?month=09-2016)
* [ July 2016 ](https://snprotips.com/blog?month=07-2016)
* [ May 2016 ](https://snprotips.com/blog?month=05-2016)
* May 17, 2016 [What's New in Helsinki?](https://snprotips.com/blog/2016/5/17/whats-new-in-helsinki)
* [ April 2016 ](https://snprotips.com/blog?month=04-2016)
* [ March 2016 ](https://snprotips.com/blog?month=03-2016)
* [ February 2016 ](https://snprotips.com/blog?month=02-2016)
* [ January 2016 ](https://snprotips.com/blog?month=01-2016)
* [ December 2015 ](https://snprotips.com/blog?month=12-2015)
* [ October 2015 ](https://snprotips.com/blog?month=10-2015)
* Oct 20, 2015 [Bookmarklet: Load the current page in the ServiceNow frame](https://snprotips.com/blog/rvicenowprotips.com/2015/10/bookmarklet-load-current-page-in.html)
* [ August 2015 ](https://snprotips.com/blog?month=08-2015)
* Aug 27, 2015 [Easily Clone One User's Access to Another User](https://snprotips.com/blog/rvicenowprotips.com/2015/08/clone-one-users-access-to-another-user.html)

View original source

https://snprotips.com/blog/2019/12/18/can-script-includes-use-the-current-variable