Introduction
Glide is an extensible Web 2.0 development
platform written in Java
that facilitates rapid development of forms-based workflow applications
GlideRecord to Query Tables
In order to query
a table, first create a ServiceNow object
for the table. This object is called a GlideRecord
.
To create a GlideRecord, create the following in script:
var target = new GlideRecord('incident');
target.query(); // Issue the query to the database to get all records
while (target.next()) {
// add code here to process the incident record
}
This issues the query() to the database. Each call to next()
would load the next record.
If we wanted to find all incidents
where the priority field is GREATER THAN 1
var target = new GlideRecord('incident');
target.addQuery('priority', '>', 1);
target.query(); // Issue the query to the database to get relevant
records
while (target.next()) {
// add code here to process the incident record
}
List of all operators
addQuery('priority', '=', 1); //= Field must be equal to value supplied.
addQuery('priority', '>', 1); // > Field must be greater than value supplied
addQuery('priority', '<', 3); //< Field must be less than value supplied.
addQuery('priority', '>=', 1); //>= Field must be equal or greater than value supplied
addQuery('priority', '<=', 3); //<= Field must be equal or less than value supplied.
addQuery('priority', '!=', 1); //!= Field must not equal the value supplied.
addQuery('short_description', 'STARTSWITH', 'Error'); //STARTSWITH Field must start with the value supplied.
addQuery('short_description', 'CONTAINS', 'Error'); //CONTAINS Field must contain the value supplied somewhere in the text.
addQuery('short_description', 'IN', 'Error,Success,Failure'); //query the variable value table and supply this list of sys_ids.
addQuery('short_description', 'ENDSWITH', 'Error'); //ENDSWITH Field must terminate with the value supplied.
addQuery('short_description', 'DOES NOT CONTAIN', 'Error'); //Field must not have with the value supplied anywhere in the text
addQuery('short_description', 'NOT IN', 'Error,Success,Failure'); //get all records where the short_description field does not have the word
addQuery('sys_class_name', 'INSTANCEOF', 'cmdb_ci_computer'); //INSTANCEOF for a specified "class" for tables
Null and Not null check
var target = new GlideRecord('incident');
target.addNullQuery('short_description');
target.query(); // Issue the query to the database to get all
records
while (target.next()) {
// add code here to process the incident record
}
var target = new GlideRecord('incident');
target.addNotNullQuery('short_description');
target.query(); // Issue the query to the database to get all
records
while (target.next()) {
// add code here to process the incident record
}
query
var rec = new GlideRecord('incident');
rec.query();
while (rec.next()) {
gs.print(rec.number + ' exists');
}
update
var rec = new GlideRecord('incident');
rec.addQuery('active', true);
rec.query();
while (rec.next()) {
rec.active = false;
gs.print('Active incident ' + rec.number = ' closed');
rec.update();
}
insert
var rec = new GlideRecord('incident');
rec.initialize();
rec.short_description = 'Network problem';
rec.caller_id.setDisplayValue('Joe Employee');
rec.insert();
delete
var rec = new GlideRecord('incident');
rec.addQuery('active', false);
rec.query();
while (rec.next()) {
gs.print('Inactive incident ' + rec.number + ' deleted');
rec.deleteRecord();
}
Active Inactive state
inc.addActiveQuery(); //filter to return active records.
inc.addInactiveQuery(); //records where the active flag is false.
Has Attachments
True if the current record has attachments
.
//Check for attachments and add link if there are any
var attachment_link = '';
var rec = new GlideRecord('sc_req_item');
rec.addQuery('sys_id', current.request_item);
rec.query();
if (rec.next()) {
if (rec.hasAttachments()) {
attachment_link = gs.getProperty('glide.servlet.uri') +
rec.getLink();
}
}
Add Query
1 argument adds an encoded query string. 2 arguments return records where the field is equal to the value (or is in a list of values). 3 arguments return records where the field meets the specified condition (field, operator and value).
rec.addQuery('active', true);
rec.addQuery('sys_created_on', ">", "2010-01-19 04:05:00");
que.addQuery('number', 'IN', 'INC00001,INC00002');
Join tables
Parameters:
joinTable – table name
.
primaryField (optional) – if other than sys_id
, the primary field.
joinTableField (optional) – if other than sys_id
, the field that joins
the tables
.
// Look for Problem records
var gr = new GlideRecord('problem');
// That have associated Incident records
var grSQ = gr.addJoinQuery('incident');
// Where the Problem records are "active=false"
gr.addQuery('active', 'false');
// And the Incident records are "active=true"
grSQ.addCondition('active', 'true');
// Query
gr.query();
// Iterate and print results
while (gr.next()) {
gs.print(gr.getValue('number'));
}
Encoded query
An encoded query
string to add to the record.
Use the breadcrumbs and filters to generate encoded query strings.
var queryString = "priority=1^ORpriority=2";
gr.addEncodedQuery(queryString);
Get Attribute
Gets the attributes
on the field in question from the dictionary.
doit();
function doit() {
var gr = new GlideRecord('sys_user');
gr.query("user_name", "admin");
if (gr.next()) {
gs.print("we got one");
gs.print(gr.location.getAttribute("tree_picker"));
}
}
Display Value
Gets the attributes
on the field in question from the dictionary.
// list will contain a series of display values separated by a comma
// array will be a javascript array of display values
var list = current.watch_list.getDisplayValue();
var array = list.split(",");
for (var i = 0; i < array.length; i++) {
gs.print("Display value is: " + array[i]);
}
Get Fields
Retrieves a Java ArrayList of fields in the current record.
// This can be run in "Scripts - Background" for demonstration purposes
// Get a single incident record
var grINC = new GlideRecord('incident');
grINC.query();
grINC.next();
gs.print('Using ' + grINC.getValue('number'));
gs.print('');
// getFields() returns a Java ArrayList
var fields = grINC.getFields();
// Enumerate GlideElements in the GlideRecord object that have values
gs.print('Enumerating over all fields with values:');
for (var i = 0; i < fields.size(); i++) {
var glideElement = fields.get(i);
if (glideElement.hasValue()) {
gs.print(' ' + glideElement.getName() + '\t' + glideElement);
}
}
gs.print('');
// Get a specific GlideElement: number
gs.print('Getting the number field:');
for (var i = 0; i < fields.size(); i++) {
var glideElement = fields.get(i);
if (glideElement.hasValue() && glideElement.getName() == 'number') {
gs.print(' ' + glideElement.getName() + '\t' + glideElement);
}
}
Get Record ClassName
Retrieves
the class name for the current
record.
function TaskAssignmentFilter() {
var classname = current.getRecordClassName();
var filter = "type=null";
if (classname == "incident" && current.category == "database") {
filter = GetGroupFilter("database");
} else {
// append exclusion for 'catalog' to the filter
var cat = new GlideRecord("sys_user_group_type");
cat.addQuery("name", "catalog");
cat.query();
if (cat.next()) {
filter += "^ORtype!=" + cat.sys_id;
}
}
gs.log("TaskAssignmentFilter: " + filter);
return filter;
}
Get TableName
Retrieves
the table name associated with this GlideRecord.
gs.log('Table: ' + current.getTableName());
gs.log('Parent: ' + current.parent.sys_id);
var item = new GlideRecord('sc_req_item');
item.addQuery('sys_id', current.parent.sys_id);
item.query();
if (item.next()) {
for (var variable in item.variable_pool) {
gs.log(variable);
var answer = eval("item.variable_pool." + variable +
".getDisplayValue()");
gs.log(answer);
}
}
Set Workflow
Enables
or disables
the running of business rules that might normally be triggered by subsequent actions. If the
parameter is set to false, an insert/update will not be audited. Auditing only happens when the parameter is
set to true for a GlideRecord operation.
Parameters:
– Boolean variable that if true (default) enables business rules, and if false to disables them.
doit('name1', 'name2');
function doit(username1, username2) {
var usr1 = new GlideRecord('sys_user');
var usr2 = new GlideRecord('sys_user');
var num = 0;
if (usr1.get('user_name', username1) &&
usr2.get('user_name', username2)) {
var ref;
var dict = new GlideRecord('sys_dictionary');
dict.addQuery('reference', 'sys_user');
dict.addQuery('internal_type', 'reference');
dict.query();
while (dict.next()) {
num = 0;
ref = new GlideRecord(dict.name.toString());
ref.addQuery(dict.element, usr1.sys_id);
ref.query();
while (ref.next()) {
ref.setValue(dict.element.toString(), usr2.sys_id);
ref.setWorkflow(false);
ref.update();
num++;
}
if (num > 0) {
gs.print(dict.element + ' changed from ' + usr1.user_name +
' to ' + usr2.user_name + ' in ' + num + ' ' + dict.name + '
records ');
}
}
}
Update With References
Updates a record
and also inserts or updates any related records with the information provided.
- if processing a incident where the
Caller ID
is set to reference sys_user record David Loo then the following code would update David Loo’s user record. - if processing a incident where there is
no Caller ID
specified, then the following code would create a new sys_user record with the provided information (first_name
,last_name
) and set the Caller ID value to the newly createdsys_user
record.
var inc = new GlideRecord(‘incident’);
inc.get(inc_sys_id); // Looking up an existing incident record where
'inc_sys_id'
represents the sys_id of a incident record
inc.caller_id.first_name = 'John';
inc.caller_id.last_name = 'Doe';
inc.updateWithReferences();
}
Insert With References
Inserts a new record and also inserts or updates any related records with the information provided.
If a reference value is not specified (as below), then a new user record will be created with the provided
first_name
, last_name
, and the caller_id value is set to this newly created sys_user
record.
The result is a new sys_user record with the provided first_name
, last_name and a new incident record with
the provided short_description
and caller_id
.
var inc = new GlideRecord(‘incident’);
inc.initialize();
inc.short_description = 'New incident 1';
inc.caller_id.first_name = 'John';
inc.caller_id.last_name = 'Doe';
inc.insertWithReferences();
}
Delete Multiple
Deletes multiple records according to the current where
clause. Does not delete
attachments.
function nukeCart() {
var cart = getCart();
var id = cart.sys_id;
var kids = new GlideRecord('sc_cart_item');
kids.addQuery('cart', cart.sys_id);
kids.deleteMultiple();
}
Set Limit
Sets the limit
for how many records are in the GlideRecord.
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(10);
gr.query();
GlideAggregate
The GlideAggregate
class is an extension of GlideRecord and allows database aggregation (COUNT
, SUM
, MIN
,
MAX
, AVG
) queries to be done.
an example to get a count of the number of active incidents.
var count = new GlideAggregate('incident');
count.addQuery('active', 'true');
count.addAggregate('COUNT', 'category');
count.query();
while (count.next()) {
var category = count.category;
var categoryCount = count.getAggregate('COUNT', 'category');
gs.log("The are currently " + categoryCount + " incidents with a
category of " + category);
}
MIN, MAX, and AVG
Records have been modified and we want the MIN
, MAX
, and AVG
values
var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();
while (count.next()) {
var min = count.getAggregate('MIN', 'sys_mod_count');
var max = count.getAggregate('MAX', 'sys_mod_count');
var avg = count.getAggregate('AVG', 'sys_mod_count');
var category = count.category.getDisplayValue();
gs.log(category + " Update counts: MIN = " + min + " MAX = " + max +
" AVG = " + avg);
}
Compare activity from one month to the next
var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category');
agg.orderByAggregate('count', 'category');
agg.orderBy('category');
agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)');
agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)');
agg.query();
while (agg.next()) {
var category = agg.category;
var count = agg.getAggregate('count', 'category');
var query = agg.getQuery();
var agg2 = new GlideAggregate('incident');
agg2.addAggregate('count', 'category');
agg2.orderByAggregate('count', 'category');
agg2.orderBy('category');
agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
agg2.addEncodedQuery(query);
agg2.query();
var last = "";
while (agg2.next()) {
last = agg2.getAggregate('count', 'category');
}
gs.log(category + ": Last month:" + count + " Previous Month:" + last);
}
Add Aggregate
String agg
– name of aggregate to add
.
String name
– name of column to aggregate
.
function doMyBusinessRule(assigned_to, number) {
var agg = new GlideAggregate('incident');
agg.addQuery('assigned_to', assigned_to);
agg.addQuery('category', number);
agg.addAggregate("COUNT");
agg.query();
var answer = 'false';
if (agg.next()) {
answer = agg.getAggregate("COUNT");
if (answer > 0)
answer = 'true';
else
answer = 'false';
}
return answer;
}
Get Aggregate
Parameters:
agg
– String type of the aggregate
(e.g. SUM
or COUNT
)
name
– String name of the field to get aggregate from.
function doMyBusinessRule(assigned_to, number) {
var agg = new GlideAggregate('incident');
agg.addQuery('assigned_to', assigned_to);
agg.addQuery('category', number);
agg.addAggregate("COUNT");
agg.query();
var answer = 'false';
if (agg.next()) {
answer = agg.getAggregate("COUNT");
if (answer > 0)
answer = 'true';
else
answer = 'false';
}
return answer;
}
Group By
Provide the name of a field to use in grouping
the aggregates..
var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();
while (count.next()) {
var min = count.getAggregate('MIN', 'sys_mod_count');
var max = count.getAggregate('MAX', 'sys_mod_count');
var avg = count.getAggregate('AVG', 'sys_mod_count');
var category = count.category.getDisplayValue();
gs.log(category + " Update counts: MIN = " + min + " MAX = " + max +
" AVG = " + avg);
}
Order By
Provide the name of a field that should be used to order
the aggregates. The field will also be added to the
group-by list.
var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category');
agg.orderByAggregate('count', 'category');
agg.orderBy('category');
agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)');
agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)');
agg.query();
while (agg.next()) {
var category = agg.category;
var count = agg.getAggregate('count', 'category');
var query = agg.getQuery();
var agg2 = new GlideAggregate('incident');
agg2.addAggregate('count', 'category');
agg2.orderByAggregate('count', 'category');
agg2.orderBy('category');
agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
agg2.addEncodedQuery(query);
agg2.query();
var last = "";
while (agg2.next()) {
last = agg2.getAggregate('count', 'category');
}
gs.log(category + ": Last month:" + count + " Previous Month:" +
last);
}
Order By Aggregate
Parameters:
agg
– String type of aggregate (e.g. SUM
, COUNT
, MIN
, MAX
)
name
– String name of field to aggregate
var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category');
agg.orderByAggregate('count', 'category');
agg.orderBy('category');
agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)');
agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)');
agg.query();
while (agg.next()) {
var category = agg.category;
var count = agg.getAggregate('count', 'category');
var query = agg.getQuery();
var agg2 = new GlideAggregate('incident');
agg2.addAggregate('count', 'category');
agg2.orderByAggregate('count', 'category');
agg2.orderBy('category');
agg2.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(3)');
agg2.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(3)');
agg2.addEncodedQuery(query);
agg2.query();
var last = "";
while (agg2.next()) {
last = agg2.getAggregate('count', 'category');
}
gs.log(category + ": Last month:" + count + " Previous Month:" +
last);
}
Conclusion
That’s all for now…Do check out other script related post here
- 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