ServiceNow Snippets · LessonReviewed by Sneha I, ServiceNow Trainer, 8 yrs · Updated · Published · current release · reference
ServiceNow GlideRecord snippets
Short, runnable GlideRecord snippets to copy.
Quick answer
Two small GlideRecord snippets to run and tweak. Delete matching records: query incidents with state 7 and call deleteMultiple, left commented out here so nothing is removed by accident. Count without looping: GlideAggregate with addAggregate('COUNT'), query, next, then getAggregate('COUNT'), which returns the total in one round trip.
Key takeaways
- Delete matching records
- Count without looping
Small, focused snippets. Run and tweak the one below.
Delete matching records
var gr = new GlideRecord('incident');
gr.addQuery('state', 7); // closed
gr.query();
// gr.deleteMultiple();Count without looping
var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT');
ga.query();
ga.next();
gs.info(ga.getAggregate('COUNT'));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.