Script examples

Having introduced the basic elements of client-side scripting and how and where to use it, we can take a look at some examples of scripts to further our understanding.

We'll start by looking at some client script examples.

In this first example, we'll use an onLoad client script to show and hide form sections based on the logged-in users' roles. We'll only show the related records form section on the incident form if the logged-in user has the itil_admin role:

function onLoad() {    if (g_user.hasRole('itil_admin')) {        g_form.setSectionDisplay('related_records', false);    }}

As you can see in the example, we are using g_user's hasRole method to determine whether the logged-in user has the required role. If they don't, then we use ...

Get Mastering ServiceNow Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.