Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Mastering ServiceNow Scripting
Mastering ServiceNow Scripting

Mastering ServiceNow Scripting: Leverage JavaScript APIs to perform client-side and server-side scripting on ServiceNow instances

eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Mastering ServiceNow Scripting

Getting Started

Welcome. In this book, I hope to teach you and enhance your ServiceNow scripting to ensure you can really be a scripting master. I will show when to configure and when to customize ServiceNow with scripts to achieve your goals. Building on the glide classes of ServiceNow, client- and server-side scripting will also be explored and explained.

Advancing through Jelly script and custom pages, this book also looks at debugging and best practices, rounding off with how to build your own custom applications.

Starting off in this first chapter, we look at when it is appropriate to script, and why scripting should be used, as well as configuring or customizing a ServiceNow instance. We will also explore the basic types of scripts in ServiceNow and when they are run. This chapter also covers the running order of ServiceNow scripts, a brief introduction to scripting, and the script editor used for scripting.

The topics we will cover in this chapter are:

  • Configuration versus customization
  • When to script in ServiceNow
  • Script types
  • Order of execution of scripts in ServiceNow
  • Basics of scripting
  • Script editor

Configuration versus customization

ServiceNow instances are complex. There are a great many ways in which they can be changed, and often, there are several ways to achieve the same goal using different techniques. These changes can be done through configuration and customization.

Configuration uses the ServiceNow interface to set up rules, conditions, and other configurations, like global system properties and filters. This is often made by using a series of drop-down lists.

We can see an example of this in a business rule as follows:

Figure 1.1: Configuration example from a business rule

In Figure 1.1 we can see filter conditions and and the values to set based on the filter. This configuration example uses no code and is preferable to customization. This type of filtering is seen across the ServiceNow platform.

Most system administrators will be able to administer their instance with configuration alone. A configured instance will suffice for a certain level of functionality on an instance, and, if you are trying to achieve a goal where configuration is available, it is usually the best option.

Customization is when an administrator uses scripts to allow an instance to perform further tasks beyond what configuration can do alone. ServiceNow is extremely open-ended, and the ability to write scripts at multiple points when loading and submitting forms makes it very versatile.

A customization might be to write a script to add a date validation on a field and show a message or clear the field if an incorrect value is entered. Custom scripts are to be managed by the creator, as ServiceNow is not responsible for the code. Therefore, if you start to move into the customization route, make sure you know what you are doing.

As the creator will need to maintain the script created, they will also need to ensure that it will still function when an instance of ServiceNow is upgraded.

If customizing an existing ServiceNow script, rather than customizing the script itself, copy it, rename the copied script, and deactivate the existing script. Then you can make as many changes to the new copied script as you like, while having the original backed up in case it is needed.

To script or not to script?

For new administrators, it can be difficult to know when to script. For coders, it can be easy to look to script before you need to, with so many opportunities to write script in ServiceNow. However, it is much better to not write script wherever possible.

If you can configure instead of scripting, it should be your first thought. This allows the tool to be used in the way it was intended and will leave you much more prepared when upgrading your instance. Not only that; one of the main reasons for using configurations over customizing is for maintenance. Configurations are easily maintained by different admins, while customization's require some basic knowledge of scripts and the logic behind them, so they tend to be harder to maintain and troubleshoot. Remember, once you start to customize, maintaining that customiszation is your responsibility. 

If you find yourself unable to achieve your goals with configuration alone, then you should look to script. Even though configuration is your best option, scripting accounts for a lot of advanced functionality on most instances. Almost all mature instances will have some level of scripting done to them, but the instances that function better are the ones where scripting was performed when appropriate.

For example, if you are looking to show, hide, or make mandatory or read-only a field, then this could be done as a client script or a UI policy. If you simply needed to perform one of the actions based on a value in a field, then in this instance, a UI policy is the better choice, as it can achieve the goal without using a script. However, if you needed to perform the action based on whether the logged-in user has a particular role, then you will need to use a script. Scripting can be done in a UI policy, but I usually opt for a client script in this scenario.

Try to avoid scripting wherever possible. Configuring an instance instead has many benefits and makes an instance easier to maintain.

Types of script

There are a multitude of different scripts you can write in ServiceNow, and the times at which they run will often dictate which is best. However, all of these scripts will fall under two categories. These are client-side scripts and server-side scripts.

These two script types will be explored further in subsequent chapters, but we will look at the basic definitions here:

  • A client-side script will run in front of the user, based on the data that was delivered to the user on the web page, usually to a form or a list, in that no form submissions are required. These scripts can only use the data loaded as part of the web page to run their scripts with (if they only run on the client side), as that is the only data available. The most common client-side script is simply named a client script in ServiceNow. Some common uses for these scripts would be to draw attention to a field to change or validate a field's value.
  • A server-side script will run behind the scenes once a form is submitted or a different trigger occurs. As this type of script is run on the server, it can use all the data held in the ServiceNow database, rather than just what was loaded on the web page. The business rule is the most commonly used server-side script. A business rule has many ways of functioning, but will usually run after a form submission, with common tasks to amend field values or update parent or child records.

The following table shows the most common types of scripts and whether they run on the client or server side:

Client side

Server side

Client Scripts

Business Rules

UI Policies

Access Controls

UI Actions

Script Includes

 

UI Actions

 

Scheduled Jobs

 

Background Scripts

 

Workflow Scripts

Script Actions

 

You may notice from the preceding table that UI Actions appear in both the client and server side. This is because they can be run as either, and therefore, they fit into both categories. We'll discuss this and the other common script types more in later chapters.

All of the script types in the preceding table will be looked at in further detail later on, and each has an important role to play in making the most of your ServiceNow instance.

Server-side scripts are considered preferable where possible, as they can run in the background away from the user, whereas client-side scripts run in front of the user and often cause the most delay in loading pages.

It is possible for client-side scripts to call server-side scripts. This will usually result in a slight delay as the information is gathered from the database. This type of server call from a client script is best avoided where possible, but often, it is necessary. Later on, we'll discuss how to best call a server-side script from the client without creating long delays for the user.

Client- and server-side scripts are a huge part of ServiceNow scripting, and the ways you can manipulate both to your advantage will determine your overall success in scripting in ServiceNow.

When writing a new script, ask yourself whether the result needs to be shown immediately in front of the user. If not, consider a server-side script rather than a client-side script.

Script execution

The order that a script executes in ServiceNow can be extremely important. Subsequent running scripts can undo or alter the changes a previous script has made.

Some scripts can be ordered by administrators, while others cannot. It is important to know how ordering your script will affect the outcome of the script. For a script that cannot be ordered it will need to run in any order compared with other baseline or custom scripts and still function correctly.

When scripts can be ordered, they are run based on the order number assigned to them. Scripts are run in ascending order, so a script of order 50 would run before a script of order 100. Each number is not unique, though, so you are able to have multiple scripts run at order 100, which is the default for a new script. In this scenario, with scripts with the same order number, you cannot be certain which order they will run in.

Consider a scenario where script A is already in existence. Let's say that script A sets a user's active field to true. As an administrator, I write script B to set a user's active field to false. If script B has a lower order than script A, it will have no effect. This can easily be misinterpreted as script B not working correctly, but it is just being overwritten by script A. If script B is at a higher order than script A, then script B will look to be working correctly. However, script A is then redundant. 

It isn't often that two scripts fly so obviously in the face of each other, but hidden among other code and multiple scripts, the above scenario is quite common in a more complicated guise. Ordering is therefore very important and quite a common reason for problems with scripts. To ensure you do not have issues with ordering in scripts, ensure you are aware of other scripts running on the field or fields you are working with before making changes with your own script.

As stated earlier, sometimes, you cannot order scripts. An example of this is for client scripts. These will essentially run in a random order, and therefore, when writing them, an administrator will need to take this into account. This means you cannot write a client script that relies on values or fields from another client script, otherwise it may break (if execution is not in the order you had hoped).

Most server-side code can be ordered, however. The order in which server-side scripts are run can be seen in the following diagram:

Figure 1.2: Execution order of scripts

As you can see in the preceding figure, there are a number of different times at which scripts are run, and therefore, it is important for an administrator to determine the right order for their script.

There are two significant points to note from this ordering system. The first is that, barring email notifications, a script can be called before or after the database operation. Selecting the right script execution timing can help streamline an instance. Selecting the wrong execution time will usually not cause a problem, but can be rather inefficient. Generally, it is a best practice to call a script before the database operation if the script will change values about the current record; otherwise, the script can be run after the database operation.

The second significant point is that order 1000 is an important number in ServiceNow. When I first started using ServiceNow, I wondered why so many scripts were ordered with such a high number. The reason is to run scripts in front of or behind engines. These engines include SLA, approval, and workflow engines. The main reason I have come across in my experience is to order scripts in relation to workflow scripts.

Script execution order bugs can be difficult to diagnose, as it can take a long time finding what other scripts are interfering with the current script you are working on. That is why it is important to label all code clearly and write meaningful comments within your scripts.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Customize your ServiceNow instance according to your organization’s needs
  • • Learn to work with inbuilt JavaScript APIs in ServiceNow
  • • Take your ServiceNow experience to the next level by learning to script

Description

Industry giants like RedHat and NetApp have adopted ServiceNow for their operational needs, and it is evolving as the number one platform choice for IT Service management. ServiceNow provides their clients with an add-on when it comes to baseline instances, where scripting can be used to customize and improve the performance of instances. It also provides inbuilt JavaScript API for scripting and improving your JavaScript instance. This book will initially cover the basics of ServiceNow scripting and the appropriate time to script in a ServiceNow environment. Then, we dig deeper into client-side and server-side scripting using JavaScipt API. We will also cover advance concepts like on-demand functions, script actions, and best practices. Mastering ServiceNow Scripting acts as an end-to-end guide for writing, testing, and debugging scripts of ServiceNow. We cover update sets for moving customizations between ServiceNow instances, jelly scripts for making custom pages, and best practices for all types of script in ServiceNow. By the end of this book, you will have hands-on experience in scripting ServiceNow using inbuilt JavaScript API.

Who is this book for?

This book is targeted toward ServiceNow administrators or anyone willing to learn inbuilt JavaScript APIs used to script and customize ServiceNow instances. Prior experience with ServiceNow is required.

What you will learn

  • • Customize your ServiceNow instance according to your organization s needs
  • • Explore the ServiceNow-exposed JavaScript APIs and libraries
  • • Discover the method for using ServiceNow scripting functions
  • • Take your ServiceNow experience to the next level by understanding advanced scripting
  • • Learn to build, test, and debug custom applications
  • • Use your customized instance efficiently with the help of best practices
Estimated delivery fee Deliver to Malaysia

Standard delivery 10 - 13 business days

$8.95

Premium delivery 5 - 8 business days

$45.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 27, 2018
Length: 226 pages
Edition : 1st
Language : English
ISBN-13 : 9781788627092
Vendor :
ServiceNow
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Estimated delivery fee Deliver to Malaysia

Standard delivery 10 - 13 business days

$8.95

Premium delivery 5 - 8 business days

$45.95
(Includes tracking information)

Product Details

Publication date : Feb 27, 2018
Length: 226 pages
Edition : 1st
Language : English
ISBN-13 : 9781788627092
Vendor :
ServiceNow
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 153.97
Learning ServiceNow
$54.99
ServiceNow Application Development
$54.99
Mastering ServiceNow Scripting
$43.99
Total $ 153.97 Stars icon

Table of Contents

13 Chapters
Getting Started Chevron down icon Chevron up icon
Exploring the ServiceNow Glide Class Chevron down icon Chevron up icon
Introduction to Client-Side Scripting Chevron down icon Chevron up icon
Advanced Client-Side Scripting Chevron down icon Chevron up icon
Introduction to Server-Side Scripting Chevron down icon Chevron up icon
Advanced Server-Side Scripting Chevron down icon Chevron up icon
Introduction to Custom Pages Chevron down icon Chevron up icon
Scripting with Jelly Chevron down icon Chevron up icon
Debugging the Script Chevron down icon Chevron up icon
Best Practices Chevron down icon Chevron up icon
Deployments with the Update Sets Chevron down icon Chevron up icon
Building a Custom Application Using ServiceNow Scripting Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Most Recent
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.4
(7 Ratings)
5 star 28.6%
4 star 28.6%
3 star 14.3%
2 star 14.3%
1 star 14.3%
Filter icon Filter
Most Recent

Filter reviews by




siri Jan 05, 2022
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Not that much expected. Not preferable to purchase. Use cases are not given only explenatiin given for what is what and complete scripting also not given. Price is too high and not worthy
Amazon Verified review Amazon
Amazon Customer Apr 11, 2021
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Covers a lot that would require digging docs and community posts, and more besides.
Amazon Verified review Amazon
Yolanda Herrera Mar 16, 2020
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Es un libro que le funciona muy bien a las personas que están aprendiendo la plataforma.
Amazon Verified review Amazon
Americo Panichi Feb 02, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Fraco
Amazon Verified review Amazon
Praveen Nov 03, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good book for beginner's and a good reference for developers with SNoW experience.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela