Back to articles
The basics of Script Include in ServiceNow
Script Include 101
TLDR
- Script include is used to store JavaScript that runs on the server
- Script Include is reusable server-side script logic that defines a function or class. Script Includes executes its script logic on the server side only when explicitly called by other scripts from the Server or client sides
- There are three different types of Script Includes:
- On-demand/classless
- Define a new class
- Extend an existing class
Intro
Like business rules, Script Includes are reusable server-side script logic defining a function or class. Script Includes executes its script logic only when explicitly called by other scripts. However, business rules are not reusable server-side script logic. For more info about business rules, check here.
Types of Script Includes
On-demand/classless
- A Script Include that defines a single function is known as an on-demand, or classless, Script Include
- The function is callable from other server-side scripts
- On-demand Script Includes can never be used client-side, even if the Client callable option is selected
- The Script Include name must exactly match the name of the function
- On-demand Script Includes are typically utilized when script logic needs to be reused
- Examples include standardizing date formats, enabling/disabling logging, and validating email addresses
- Example:
- Note:
- Remove the default script and replace it with your own function
- You can call this script include through a BR
Utilities Script Include
- Utilities Script Includes typically define a new class and therefore use the automatically inserted script template
- The initialize function is automatically invoked when JavaScript objects are instantiated from the Script Include. Any variable defined as part of this object in the initialize function is known to all other functions in the Script Include
- Many apps have one or more Script Includes to store the majority of the app’s logic
- If there is a single Script Include for an app, it is often named <App Name>Utils
- Example:
- Note: add your function within the default script
- Another Script Include example (fetching a specific location):
- Call the getLoc script include in the reference qualifier:
- You could also have a scenario where you need to fetch a specific location for each domain separation/instance if you’re using a domain-separate environment
- For this, you can use advanced reference qualifiers. You can define the functionalities in a script include & call these functionalities within a reference qualifier
- Other examples:
Extended Script Include (Extend an existing class)
- Script Includes can extend existing Script Includes by adding new methods and non-method properties
- Although most SN classes are extensible, the most commonly extended classes are:
- GlideAjax: make AJAX calls from Client Scripts (client callable)
- LDAPUtils: add managers to users, set group membership, debug LDAP
- Catalog: set of classes used by the Service Catalog for form processing and UI building
- This type of script allows you to override a function
- Example:
That's a wrap!
References
- https://www.youtube.com/watch?v=0lh2LtegEag&ab_channel=ServiceNowDemo
- https://www.youtube.com/watch?v=svKX5KELh44&ab_channel=ServiceNowAdda







great blog
ReplyDeleteThe article gives a clear introduction to ServiceNow Script Includes and explains their role in storing reusable server-side JavaScript logic. The distinction between Script Includes and Business Rules is useful, particularly the point that Script Includes execute when explicitly called rather than automatically as part of a record-processing event.
ReplyDeleteThe explanation of on-demand or classless Script Includes also shows how a single reusable function can support common tasks such as validation, formatting, and logging. These examples provide a practical context for Javascript Online Course concepts involving reusable functions, server-side scripting, and structured application logic.
The discussion of Utilities Script Includes adds another useful perspective by explaining how classes, the initialize function, and shared object variables can organize application logic. Understanding these JavaScript fundamentals through Javascript Online Training can help when working with reusable scripts and larger ServiceNow applications.
ReplyDelete