IT CanvassTalk to an advisor
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.
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