32,39 €
Understand the ServiceNow scripting and build an efficient customized ServiceNow instance
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.
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.
Andrew Kindred moved on to ServiceNow from his background as a programmer and now has over 6 years' experience working with ServiceNow in a range of industry sectors including finance, government, and media. He is a certified ServiceNow implementation specialist and application developer, currently working in investment banking in London, overseeing and developing implementations and customizations of ServiceNow.Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 235
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor:Gebin GeorgeAcquisition Editor: Heramb BhavsarContent Development Editor: Devika BattikeTechnical Editor: Prachi SawantCopy Editors: Safis Editing, Dipti MankameProject Coordinator: Judie JoseProofreader: Safis EditingIndexer: Rekha NairGraphics: Tom ScariaProduction Coordinator: Nilesh Mohite
First published: February 2018
Production reference: 1260218
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78862-709-2
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Andrew Kindred moved on to ServiceNow from his background as a programmer and now has over 6 years' experience working with ServiceNow in a range of industry sectors including finance, government, and media.
He is a certified ServiceNow implementation specialist and application developer, currently working in investment banking in London, overseeing and developing implementations and customizations of ServiceNow.
Nabil Oukelmoune is a solution architect, an SME with multiple awards in several business areas, an expert in providing IT services and solutions, and is also developing new and innovative processes for many clients across EMEA, the USA, and Asia.
He graduated with honors from the IIHEM with a BBA majoring in management information systems. He has more than 9 years of proven achievements in IT&S including infrastructure management, multi-tiered technical support, process analysis, pm, and solution and enterprise architecture.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Mastering ServiceNow Scripting
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Disclaimer
Getting Started
Configuration versus customization
To script or not to script?
Types of script
Script execution
Introduction to scripting
Script editor
Summary
Exploring the ServiceNow Glide Class
Using ServiceNow exposed JavaScript APIs
GlideRecord
Server-side Glide classes
GlideSystem
GlideDateTime
GlideElement
GlideAggregate
Client-side Glide classes
GlideForm
GlideUser
Additional client-side Glide classes
Script examples
Summary
Introduction to Client-Side Scripting
Client scripts
onLoad
onChange
onSubmit
onCellEdit
UI policies
How and when to write client-side scripts
Making fields mandatory, visible, or read-only
Populating fields based on the value of another field
Showing/hiding form sections
Review
Testing client-side scripts
Alert
Jslog
Browser
Script examples
Summary
Advanced Client-Side Scripting
UI actions
Client-side UI actions
Calling server-side script
AJAX calls
Client-side AJAX
Server-side AJAX 
Script examples
Summary
Introduction to Server-Side Scripting
Business rules
UI actions
Access controls
How and when to write server-side scripts
Changing form values
Restricting access to forms and fields
Passing values to the client side
Review
Testing server-side scripts
gs.log
Logging in scoped applications
Session debug
Script debugger
Script examples
Summary
Advanced Server-Side Scripting
Script includes
Scheduled jobs
Background scripts
Workflow scripts
Event management
Script actions
Script examples
Summary
Introduction to Custom Pages
Introduction to Jelly
UI pages
Script examples
Summary
Scripting with Jelly
Jelly scripting
Evaluate
If
UI macros
Script examples
Summary
Debugging the Script
The script debugger
The session debugger
The Field Watcher
Debugging applications
The JavaScript Log and Debug window
Debugging REST
SOAP debugging
Summary
Best Practices
Coding best practices
Business rule best practices
Before
After
Async
Display
Client script best practices
Debugging tools best practices
Logging
Session debug
Logs and queues best practices
Logs
Queues
Event queue
System scheduler
ECC queue
System performance best practices
Summary
Deployments with the Update Sets
When to use update sets
How to use scope with update sets
Transferring update sets between instances
Update set pitfalls
Update set best practices
Summary
Building a Custom Application Using ServiceNow Scripting
Creating a custom application with ServiceNow scripting
Creating the application
Building the application
Example application
End-to-end development
Studio
Application development
Example application
Testing
Example application
Deployment
Publish to an update set
Publish to the application repository 
Publish to the ServiceNow store
Example application
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
ServiceNow is gradually evolving as the platform of choice for IT Service management. Industry giants such as RedHat and NetApp have adopted ServiceNow for their operational needs. ServiceNow provides clients with an extra add-on when it comes to their baseline instances, as scripting can be used to customize and improve the performance of their instances. ServiceNow provides inbuilt JavaScript API for scripting and improving an instance using JavaScript.
This book will initially cover the basics of ServiceNow scripting and the appropriate time to script in a ServiceNow environment. Then, we will dig deeper into client-side and server-side scripting using JavaScipt API. We will also cover advanced concepts such as on-demand functions, script actions, and best practices. This book will act as an end-to-end guide to writing, testing, and debugging scripts in ServiceNow. We will also 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.
At the end of this book, you will have hands-on experience of scripting in ServiceNow using the inbuilt JavaScript API.
This book is targeted toward ServiceNow administrators or any stakeholder willing to learn inbuilt JavaScript APIs used to script and customize ServiceNow instances. Experience of working with ServiceNow is mandatory.
Chapter 1, Getting Started, introduces you to the basics of ServiceNow scripts. That is, when/why it is appropriate to develop custom functionality through scripting. It also introduces when to configure and customize.
Chapter 2, Exploring the ServiceNow Glide Class, provides you with the details of how ServiceNow has exposed its JavaScript APIs, which enables you to write your scripts conveniently. Using these exposed APIs, you can perform various database operations. You will explore some of the commonly used server-side glide classes and client-side glide classes.
Chapter 3, Introduction to Client-Side Sripting, helps you understand the client-side scripting of ServiceNow. You will learn about the concepts of client scripts and UI policies. You will see how to write and test basic client-side scripts. You will go through some practical examples of client-side scripting to have better understanding of the functionality.
Chapter 4, Advanced Client-Side Scripting, will help you understand the more advanced side of client-side scripting in ServiceNow. You will learn about AJAX calls and UI actions. You will go through some advanced practical examples of client-side scripting to get a better understanding of the functionality.
Chapter 5, Introduction to Server-Side Scripting, covers the details of server-side scripting in ServiceNow. It will help you understand the concepts of business rules, UI actions, and access controls in depth. You will also learn how to write and test a server-side script. You will go through some of the practical examples of server-side scripting to understand the functionality better.
Chapter 6, Advanced Server-Side Scripting, will cover the advanced side of server-side scripting in ServiceNow. It will help you understand the concepts of script includes, background scripts, workflow scripts, and scheduled jobs. You will go through some practical examples of advanced server-side scripting to understand script calls, the system scheduler, and queuing events in the system.
Chapter 7, Introduction to Custom Pages, introduces you to Jelly. This will provide an insight into how Jelly is used in ServiceNow. You will also learn how to create a UI page using JavaScript with Jelly scripting.
Chapter 8, Scripting with Jelly, further progresses knowledge of Jelly. This will provide further insight into how Jelly scripting is used in ServiceNow. You will also learn how to create UI Macros to enhance a UI page.
Chapter 9, Debugging the Script, introduces the mechanism of debugging your script in ServiceNow. You will explore the various tools and methods used in troubleshooting and debugging your code in ServiceNow.
Chapter 10, Best Practices, covers various best practices developers should follow to use ServiceNow in an efficient manner. It also talks about logging and monitoring system performance to control your ServiceNow environment.
Chapter 11, Deployments with the Update Sets, guides you on how to move your configurations and customizations from instance to instance. It also helps you understand how to use update sets while working on global and scoped applications. You will also learn how to avoid some common pitfalls while working with update sets.
Chapter 12, Building a Custom Application Using ServiceNow Scripting, provides you with an end-to-end implementation of scripting in ServiceNow. You will learn how to build a custom application using scripting provided by ServiceNow.
Before starting with Mastering ServiceNow Scripting, it is advised that you have a good understanding of the ServiceNow platform. It is advisable to have a system administrator certification or similar knowledge, and to be aware of forms, lists, and tables in ServiceNow. You should also be able to comfortably navigate around a ServiceNow instance.
Some knowledge of JavaScript will be advantageous, but is not compulsory as the examples given will give you script to try and get started with.
It is advised to have a ServiceNow instance that you can use to try out the examples and create some scripts of your own. If you do not have an instance at this point, personal developer instances (PDI's) can be requested from ServiceNow for users who would like to improve their ServiceNow skills.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Mastering-ServiceNow-Scripting. In case, there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/MasteringServiceNowScripting_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
The code supplied in this book is for use in development scenarios to further knowledge of ServiceNow scripting. Use of the code provided in this book is at the readers own discretion. The author and publisher are not responsible and do not accept liability for any damages or negative impacts brought about from any code used from this book.
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
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:
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.
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.
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.
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.
