IT CanvassTalk to an advisor
ServiceNow Cheat Sheets · LessonReviewed by Praveen T, ServiceNow Trainer, 9 yrs · Updated · Published · current release · reference

ServiceNow GlideRecord cheat sheet

Every GlideRecord pattern on one page.

Quick answer

The GlideRecord pattern in three moves. Query: new GlideRecord('incident'), addQuery for each condition, orderByDesc, setLimit, then query(). Iterate: while gr.next(), read fields with getValue. Write: initialize(), setValue, then insert(), or update() and deleteRecord() on a record you have fetched. Every snippet on this page runs in the editor beside it, so change a value and try it.

Key takeaways
  • Query
  • Iterate
  • CRUD

Bookmark this. Run any snippet in the editor below.

Query

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.addQuery('priority', '<=', 2);
gr.orderByDesc('sys_created_on');
gr.setLimit(10);
gr.query();

Iterate

while (gr.next()) {
  gs.info(gr.number + ' ' + gr.getValue('short_description'));
}

CRUD

gr.initialize();
gr.setValue('short_description', 'New');
var id = gr.insert();
// gr.update();  gr.deleteRecord();
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