0% found this document useful (0 votes)
0 views4 pages

Script Include 02

A Script Include in ServiceNow is a reusable server-side JavaScript code container that allows for the storage of functions or classes for use in other scripts, promoting code reuse and maintenance. There are three types of Script Includes: On-Demand, Client-Callable, and Class-Based, with On-Demand Script Includes containing a single reusable function. An example use case demonstrates how to update Problem records with Incident work notes using an On-Demand Script Include and an After Business Rule.

Uploaded by

vagesh hiremath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Script Include 02

A Script Include in ServiceNow is a reusable server-side JavaScript code container that allows for the storage of functions or classes for use in other scripts, promoting code reuse and maintenance. There are three types of Script Includes: On-Demand, Client-Callable, and Class-Based, with On-Demand Script Includes containing a single reusable function. An example use case demonstrates how to update Problem records with Incident work notes using an On-Demand Script Include and an After Business Rule.

Uploaded by

vagesh hiremath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Script Include in ServiceNow

Definition

A Script Include is a reusable server-side JavaScript code container in ServiceNow. It is used to store functions
or classes that can be called from other server-side scripts and, when configured as client callable, from client-
side scripts.
In simple words: Write the logic once in a Script Include and call it whenever you need it.

Why do we use Script Includes?

Instead of writing the same logic multiple times, we write the logic once in a Script Include and reuse it
whenever required.
• Avoid duplicate code
• Reuse common functionality
• Make code easier to maintain
• Organize business logic properly

Types of Script Includes

1. On-Demand / Classless Script Include


2. Client-Callable Script Include
3. Class-Based Script Include

1. On-Demand / Classless Script Include


Definition

An On-Demand Script Include, also called a Classless Script Include, contains a single reusable function.
It can be called directly from server-side scripts, such as:
• Business Rules
• UI Actions
• Scheduled Jobs
• Background Scripts
It cannot be called directly from client-side scripts.

Important Points

• It contains only one function.


• The function name and Script Include name must be the same.
• It is mainly used for reusable server-side logic.
• We can pass parameters to the function.
• It does not use a class structure.
Use Case
Requirement

When the Work notes of an Incident are updated, the same work notes should automatically be added to the
associated Problem record.
For this requirement, we create:
4. On-Demand Script Include → Contains the reusable logic to update the Problem.
5. After Business Rule → Calls the Script Include when Incident work notes change.

Step 1: Create an On-Demand Script Include


Name: updateWorkNotes
function updateWorkNotes(problemId, workNotes) {

var gr = new GlideRecord('problem');


[Link]('sys_id', problemId);
[Link]();

if ([Link]()) {
gr.work_notes = workNotes;
[Link]();
}
}

Code Explanation
function updateWorkNotes(problemId, workNotes)

Creates a function called updateWorkNotes. It accepts two parameters:


• problemId → Sys ID of the associated Problem record
• workNotes → Work notes received from the Incident
var gr = new GlideRecord('problem');

Creates a GlideRecord object for the Problem table.


[Link]('sys_id', problemId);

Searches for the Problem record whose sys_id matches the given Problem ID.
[Link]();

Executes the query.


if ([Link]())

Checks whether the Problem record is found.


gr.work_notes = workNotes;
Adds the Incident work notes to the Problem record.
[Link]();

Updates the Problem record in the database.

Step 2: Create an After Business Rule on Incident


Configuration

Table: Incident

When: After

Action: Update

Script
if (current.work_notes.changes()) {

var demo = current.work_notes.getJournalEntry(1);

updateWorkNotes(current.problem_id, demo);
}

Code Explanation
current.work_notes.changes()

Checks whether the Work notes field has changed on the Incident.
var demo = current.work_notes.getJournalEntry(1);

Gets the latest Work notes entry from the Incident.


updateWorkNotes(current.problem_id, demo);

Calls the On-Demand Script Include function and passes:


• current.problem_id → Associated Problem record
• demo → Latest Incident work notes

Complete Flow
Incident Work Notes Updated

After Business Rule executes


Gets the latest Work Notes

Calls updateWorkNotes() Script Include

Script Include searches for the associated Problem

Updates the Problem Work Notes

You might also like