Introduction:
Probably one of the most important concepts of ServiceNow for a developer to learn is Script Include.
ServiceNow comes with preloaded tons of script include that solves specific problem for the module.
Script Include help to promote reusability
, modularize components
and efficiency
of client server model in ServiceNow.
But before we start we need to Know Glide Ajax
.
Glide Ajax
Glide ajax is a class used by client-side script. It allows the execution of server-side
code from client side
We should initialize the glide ajax with script include name as a parameter which we want to use.
Parameter sysparm_name
is used to find the function of script include we want to execute.
When we have to give custom parameter then we should remember that it should start with sysparm
For example: ga.addParam('sysparam_assignedTo',emp);
Glide Ajax are two types
Asynchronous It doesn’t wait for the response from the server. It uses call back function getXml() Synchronous function is used for synchronous Glide Ajax call getXmWaitl()
Always validate, that client callable check box is checked in the script include which is used in Glide Ajax(client side)
addParam() method is used with the parameters to execute the script include method and is used to provide values.
// Here initialize the Glide Ajax with parameter as script include name (here: classdemo)
var ga = new GlideAjax('classdemo');
// Here pass the function name “getManagerName” as parameter which is part of the script include
ga.addParam('sysparm_name', 'getManagerName');
// Here we pass the additional variable
ga.addParam('sysparam_assignedTo', g_form.getValue('assigned_to'));
// Asynchronous Glide Ajax calls the call back function.
ga.getXML(classdemo);
// Body of the call backfunction
function classdemo(response) {
// Here we get the XML response and parse the answer attribute.
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_manager_name', answer);
}
Script Include
Script include is used to store Java script that runs on the server or script Includes are reusable Java script definition which can be called from any server-side script and in some cases from the client side too.It is used in business Rule, Reference qualifier, UI action and in other script Include.
While creating script include we should make sure that names of the script include prototype and type should be the same.
Script include can be Public or private. Most client callable script include are marked private by default. Also important to note that Script include is execute only when explicitly called by other scripts.
There are three different type of script includes
- On demand/classless
- Extend an existing class
- Define a new class
Extend an existing class
var Classdemo = Class.create();
Classdemo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
type: 'Classdemo'
});
Define a new class
var Classdemo = Class.create();
Classdemo.prototype = {
initialize: function() {},
type: 'Classdemo'
};
Example:
Table:Incident
When we populate assigned to
field , corresponding Manager name will get auto-populated
in a custom field Manager Name
.
Onchange client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var emp = g_form.getValue('assigned_to');
var ga = new GlideAjax('classdemo');
ga.addParam('sysparm_name', 'getManagerName');
ga.addParam('sysparam_assignedTo', emp);
ga.getXML(classdemo);
function classdemo(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_manager_name', answer);
}
}
Script Include:
var classdemo = Class.create();
classdemo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManagerName: function() {
var sysid = this.getParameter("sysparam_assignedTo");
var managername = '';
var empname = new GlideRecord('sys_user');
empname.addQuery('sys_id', sysid);
empname.query();
if (empname.next()) {
managername = empname.manager;
}
return managername.getDisplayValue();
}
});
- Understanding Request, RITM, Task in ServiceNow
- Steps to create a case in ServiceNow (CSM)
- Performance Analytics in 10 mins
- Event Management in 10 minutes - part1
- Event Management in 10 minutes - part2
- Custom Lookup List
- Script includes in 5 minutes
- Interactive Filter in 5 minutes
- UI Policy in 6 Minutes
- Client Side Script Versus Server Side Script in 3 minutes
-
Snow
- Performance Analytics
- ServiceNow Scripts
- Script include
- Useful scripts
- Basic Glide Scripts
- Client Script
- Advance Glide Script
- Glide System Script
- Admin
- Import Set
- Work Flow
- ACL
- SLA
- Notification
- Core Application
- UI Policy
- UI Action
- Client Script
- CAB Workbech
- Data Policy
- Connect Support
- Catalog
- Discovery
- CSM
- Event Management
- HR
- Integrations
- SSO Integration
- LDAP Integration
- SCCM Integration
- AWS Intergration
- Slack Integration
- CTI Integration
- Jira Integration
- Ebonding ServiceNow
- SOAP Integration
- IBM Netcool Integration
- VIP Mobile App Integration
- Rest Integration
- Service Portal
- Questions
- ACL
- Performance analytics(PA) Interactive Filter
- Various Configurations in Performance analytics(PA)
- Service Portal
- Performance Analytics(PA) Widgets
- Performance Analytics(PA) Indicator
- Performance Analytics(PA) Buckets
- Performance Analytics(PA) Automated Breakdown
- Client Script
- Rest Integration
- Understanding the Request, RITM, Task
- Service Catalogs
- Events in ServiceNow
- Advance glide script in ServiceNow
- CAB Workbench
Comments