Introduction
In this topic we discuss very basic glide quires that we can use in ServiceNow.
This scripts
are mostly for beginners in ServiceNow.
Any data operation is done via CRUD (Create
Read
Update
Delete
).ServiceNow too have a rich set of functionality in Glide Object that helps
to make changes in data model.Lets us go through few scripts below
Basic CRUD script
Add Query
var rec = new GlideRecord('incident'),
rec.addQuery('number', 'INC0012345');
rec.query();
var rec = new GlideRecord('incident');
gr.addQuery('closed_at', '>', gs.nowDateTime());
var rec = new GlideRecord('incident');
rec.addQuery('number', 'CONTAINS', '123');
Select Query
Objective: Get
all the list of incidents
// Initialize a glidequery for table incident.
// runs the query
// Loop through the results of the query
// rec is a single row of the result,in rec.number is the column name in table incident
var rec = new GlideRecord('incident');
rec.query();
while (rec.next()) {
gs.print(rec.number + ' exists');
}
Update Query
Objective: update
all active incident to inactive
// Initialize a glide query for table incident.
// Add filter with incident which are active = true
// Run the query
// Loop through the result
// Set the current rec to active false.
// Print the current rec
// Execute 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');
ec.update();
}
Insert Query
Objective: Insert
a new incident
var rec = new GlideRecord('incident');
rec.initialize();
rec.short_description = 'Network problem';
rec.caller_id.setDisplayValue('Joe Employee');
rec.insert();
Delete Query
Objective: Delete
inactive incidents
var rec = new GlideRecord('incident');
rec.addQuery('active', false);
rec.query();
while (rec.next()) {
gs.print('Inactive incident ' + rec.number + ' deleted');
rec.deleteRecord();
}
Add EncodedQuery
A easy way to use encoded query is first create the required filter in ServiceNow table.
Right click on the filter and click “copy query”
Then write the script with addEncodedQuery
and paste the query inside ()
Note: This CANNOT be used in client script and UI policy
var gr = new GlideRecord("incident");
// Copy paste from filter applied in Incident Table
gr.addEncodedQuery("active=true^priority=3");
gr.query();
while (gr.next()) {
gs.print(gr.number);
}
OR Query
The standard addQuery
parameter acts like an and
condition in our query.
This example shows how we can add ‘or’ conditions to our query.
Objective: Find all incidents with a priority of 1 or 2
var rec = new GlideRecord('incident');
rec.addQuery('priority', 1).addOrCondition('priority', 2);
rec.query();
while (rec.next()) {
//Do something with the records returned
if (rec.category == 'software') {
gs.log('Category is ' + rec.category);
}
}
Glide Aggregate
GlideAggregate
is actually an extension
of the GlideRecord object. It allows you to perform the following aggregations on query record-sets.
- COUNT
- SUM
- MIN
- MAX
- AVG
Objective: Find all active
incidents and print a count
of records
var gr = new GlideAggregate('incident');
gr.addQuery('active', true);
gr.addAggregate('COUNT');
gr.query();
var rec= 0;
if (gr.next()){
rec= gr.getAggregate('COUNT');
gs.print('Active incident count: ' + rec);
}
O/P:
[0:00:00.004] Script completed in scope global: script
*** Script: Active incident count: 2646O/P:
Objective: Get a count of all the open incidents by category
var gr = new GlideAggregate('incident');
gr.addQuery('active', true);
gr.addAggregate('COUNT', 'category');
gr.query();
while (gr.next()) {
var rec = gr.category;
var categoryCount = gr.getAggregate('COUNT', 'category');
gs.print("there are currently " + categoryCount + " incidents with a category of " + rec);
}
O/P: [0:00:00.017] Script completed in scope global: script
*** Script: there are currently 1 incidents with a category of database
*** Script: there are currently 4 incidents with a category of hardware
*** Script: there are currently 2629 incidents with a category of inquiry
*** Script: there are currently 4 incidents with a category of network
*** Script: there are currently 6 incidents with a category of software
OrderBy and OrderBy Descending
We can order the results of our record set by using orderBy
and/or orderByDesc
var gr = new GlideRecord('incident');
gr.orderBy('number');
gr.query();
while (gr.next()) {
gs.print(gr.number);
}
O/P:[0:00:00.467] Script completed in scope global: script
*** Script: INC0000001
*** Script: INC0000002
*** Script: INC0000003
*** Script: INC0000004
*** Script: INC0000005
*** Script: INC0000006
*** Script: INC0000007
*** Script: INC0000008
*** Script: INC0000009
*** Script: INC0000010
Set Limit
setLimit can be used to limit
the number of results returned
var gr = new GlideRecord('incident');
gr.orderBy('number');
gr.setLimit(10);
gr.query();
while (gr.next()) {
gs.print(gr.number);
}
O/P:[0:00:00.467] Script completed in scope global: script
*** Script: INC0000001
*** Script: INC0000002
*** Script: INC0000003
*** Script: INC0000004
*** Script: INC0000005
*** Script: INC0000006
*** Script: INC0000007
*** Script: INC0000008
*** Script: INC0000009
*** Script: INC0000010
Set Workflow
setWorkflow is used to enable/disable the running of any business rules that may be triggered by a particular update.
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
while (gr.next()) {
gr.category = 'hardware';
gr.setWorkflow(false);
gr.update();
}
Add Null & Not Null Query
When addNullQuery()
is called on a GlideRecord object, it adds a condition to your query, stating that the field passed into the method call must be null (empty).
When the addNotNullQuery()
method is called with a field name as an argument, a condition is added to your query stating just the opposite - that the field must contain a value - any value. It must be NotNull.
var rec = new GlideRecord('incident');
rec.addNullQuery('assigned_to');
rec.addNotNullQuery('assignment_group');
rec.query();
while (gr.next()) {
gs.print(rec.number);
}
In this case, query will print the incident numbers where the assignment_group field have value, but the assigned_to field does not have a value.
GetDisplayValue()
getDisplayValue() is quite simple, as it accepts no arguments, and returns the display value for the record upon which it’s called.
var rec = new GlideRecord('incident');
rec.addQuery('sys_id', '57af7aec73d423002728660c4cf6a71c');
rec.query();
if (rec.next()) {
gs.print(rec.number);
var callerid = rec.caller_id;
gs.print(callerid.getDisplayValue());
}
getValue()
getValue() returns the value of a specific field
or column
.
This can be the sys_id column, or any other column that might contain a value.
var rec = new GlideRecord('incident');
rec.addQuery('sys_id', );
var sysIDVal = gr.getValue('sys_id');
var incNumber = gr.getValue('number');
Conclusion
That’s all for now. Hope these queries will give you some idea on CRUD operation in ServiceNow. Have a look at advance glide scripts too.
- 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