Book Image

Mastering ServiceNow Scripting

By : Andrew Kindred
Book Image

Mastering ServiceNow Scripting

By: Andrew Kindred

Overview of this book

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.
Table of Contents (18 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Script examples


Now that we have seen how the basics of server-side scripting work, we can take a look at some scripting examples to further our knowledge of how to use these introductory server-side techniques.

We'll start with a further business rule example.

This time, we will create a before delete business rule to ensure that directors are not deleted. We need to set the business rule to advanced; select the delete checkbox and before in the When field. Let's have a look at the code:

(function executeRule(current, previous /*null when async*/) {

  if (current.title == 'Director') {
    gs.addErrorMessage('Cannot delete Director');
    current.setAbortAction(true);
    action.setRedirectURL(current);
  }

})(current, previous);

Here, we are checking whether the user is a director using the title field, and if so, using setAbortAction to stop the delete going ahead. To ensure the user is kept informed, we are also using addErrorMessage to display a message to the user and action.setRedirectURL...