IT CanvassTalk to an advisor
ServiceNow Examples · LessonReviewed by Neelima, ServiceNow Architect · Updated · Published · current release · reference

ServiceNow GlideRecord examples

Copy-paste GlideRecord patterns for the most common queries.

Quick answer

Three GlideRecord patterns cover most days. Query and loop: addActiveQuery, query, then while gr.next() read fields. Get one record: gr.get('number', 'INC0010023') returns true when found. Update matching records: addQuery on priority, loop, setValue and update. The sandbox on this page holds sample incident, problem and sys_user data, so run each and adapt it.

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.

Practise this on your own free instance.
Set up your free instance →
Already working on ServiceNow and stuck on a live ticket?Get an expert ServiceNow developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support