Jake Gillespie
Mega Guru

Have you ever written a Script Include that you thought was truly amazing? Reusable.. concise.. adheres to best practices.. dare I say, has some class about it? In this article we'll discuss the difference between a classy Script Include, and one lacking in class. That is, Script Includes containing Class definitions vs Script Includes with standalone function definitions.

For those new to ServiceNow, Script Includes are Javascript definitions which can be called from any server side script, and in some cases from the client side too. They are most commonly used as an alternative to Global Business Rules, since they are only loaded upon request. For example, Script Includes are most frequently called from:

  • Business Rules
  • UI Actions
  • Reference Qualifiers
  • Transform Maps
  • Other Script Includes

From the Fuji release onwards, Script Includes are created in reference to an Application Scope. This means that you can configure whether your Script Include is available globally to all applications, or only to a specific application. This helps to manage script functionality and to promote application independence. Any Script Includes created prior to the Fuji release are automatically assigned to the Global scope.

To identify whether a Script Include defines a Class or a function, we need to look at the script itself. Let's take a specific example:

Define a function that returns the current user's job title.

As you can see in the image below, function definitions are actually quite simple. It should be noted that the Script Include name must also be the same as the function name.

1.png

Classless Script Includes will generally contain a single function definition, as shown above for the function getMyTitle(). However it is possible to define multiple functions within the same Script Include.

2.png

As shown above, this is essentially the same Script Include as before, but with an additional function definition called getMyEmail(). As with the previous example, the first function name must be the same as the Script Include name. It should also be noted that the latter functions are only available for use after the first function has been called at least once. This means you won't be able to access getMyEmail() until after getMyTitle() has been called at least once prior. This process is cumbersome to say the least, and it demonstrates why it is far better to define a Class when multiple functions are required.

Defining a Script Include with a Class may seem more complex at first, but ServiceNow actually provides the initial code stub upon naming the record. Shown below is a basic Class definition.

3 v2.png

Once you have the Class definition, you just need to define your functions, however the syntax is slightly different to the standalone function definitions shown earlier. Let's take a look:

5 v2.png

Note that each function definition begins with the function name followed by a colon, and then the function keyword and parentheses. Function arguments could be added inside these parentheses as per any standalone function definition. Finally, the function definition is closed with a comma after the closing curly brace. To call getMyTitle() using the above Class, you could simply use:

4 ver2.png

Thanks to the wonders of dot-notation and object-oriented programming, any of the functions defined in the Class can be called as required using the above syntax. In technical terms, the new MyUserUtil() part is known as the Constructor, while the getMyTitle() part is known as a Method, referring to a function defined in the Class.

Another advantage to using a Class, is that you can define global properties (variables or objects etc). These are activated when the Class is first instantiated into an Object. Why is this important? Let's take a look at an expanded version of the same Class from before.

6 v2.png

In this version you'll notice a few subtle changes. Firstly, we've added the User record query to the initialize() function, and secondly the getMyTitle() and getMyEmail() functions have been trimmed down. When an initialize() function is present, it will be executed as soon as the Class is instantiated. In our example, we're retrieving the current User record within this function, and then storing it as a global property. This means that only 1 GlideRecord query is required, since the User record is then available for all functions to use (e.g getMyTitle() and getMyEmail()). This is more efficient than the previous example, which ran a GlideRecord query every time the getMyTitle() or getMyEmail() functions were called. Note that to maintain this efficiency, you will need to use the multi-line method of instantiation shown earlier.

Well there you have it, the pros and cons of using Script Includes, with and without a Class. For further information on the topics covered in this article, please see the following ServiceNow Documentation pages:

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER.

1 Comment