ServiceNow Examples · LessonBy Neelima, ServiceNow Architect · Published · Updated · current release · reference
ServiceNow GlideRecord examples
Copy-paste GlideRecord patterns for the most common queries.
Quick answer
These are the GlideRecord patterns you reach for daily. Run the example below, then adapt it, the sandbox ships with sample incident, problem and sys_user data.
Key takeaways
- Query and loop
- Get a single record
- Update matching records
These are the GlideRecord patterns you reach for daily. Run the example below, then adapt it, the sandbox ships with sample incident, problem and sys_user data.
Query and loop
var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
gs.info(gr.number + ' - ' + gr.short_description);
}Get a single record
var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0010023')) {
gs.info('Found: ' + gr.short_description);
}Update matching records
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
gr.setValue('urgency', 1);
gr.update();
}Try it YourselfJavaScript
▸ Press Run to execute.
Runs in a sandbox in your browser. Mock gs and GlideRecord and sample data are provided.
Quick check
+0 XPStreak ×0
Question 1 of 2
Which method returns a single record by field value?
Practise this on your own free instance.