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 execute their script logic on the server side only when explicitly called by other scripts from the Server or client sides
- There are 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 execute their 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
- Note: 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
Comments
Post a Comment