How do I query records with GlideRecord?
Create the object for the table, add conditions, call query() and loop with next(): var gr = new GlideRecord('incident'); gr.addEncodedQuery('active=true^priority=1'); gr.query(); while (gr.next()) { gs.info(gr.getValue('number')); }. Copy the encoded query from a list filter (right-click the breadcrumb › Copy query) so the conditions match what you saw in the list.
What is the difference between getValue() and getDisplayValue()?
getValue('caller_id') returns what is stored: the sys_id of a reference, the value of a choice (6) and dates in UTC. getDisplayValue('caller_id') returns what users see: the name, the choice label (Resolved) and dates in the user’s time zone. Both return strings. Use values for logic and comparisons, and display values for messages.
How do I count records in ServiceNow?
Use GlideAggregate: addAggregate('COUNT'), query(), then getAggregate('COUNT'). The database does the counting, and groupBy() gives one count per value. getRowCount() on a GlideRecord reads every matching row first, which is slow on big tables.
How do I update many records at once?
Either loop and call update() on each record, or set the values with setValue() and call updateMultiple(). updateMultiple() is one database call and much faster. The loop can list what it changes, count it, stop after a number of records and use setDisplayValue(), which makes it the safer choice for a first run of a data fix.
What does setWorkflow(false) do?
It stops business rules, and with them the notifications, flows, workflows and audit history they would trigger, for the changes made through that GlideRecord. It is for data fixes that should not look like user activity. Leave a comment saying why, because the next person will wonder why nothing reacted. autoSysFields(false) is the companion that keeps Updated and Updated by as they were.
What is GlideRecordSecure?
GlideRecordSecure works like GlideRecord but applies the ACLs of the user the code runs as: records and fields they may not read are left out, and changes they may not make fail. Use it in code that answers users, such as script includes called from GlideAjax, REST APIs and portal widgets. Plain GlideRecord ignores ACLs, which suits system jobs.
Should I use GlideQuery or GlideRecord?
GlideQuery (Paris and later) is a newer API on top of GlideRecord: it throws an error for a field name it does not know instead of ignoring the condition, returns JavaScript types, and reads like a chain. GlideRecord is everywhere, is what most examples use, and supports encoded queries directly. The generator writes both where GlideQuery can do the job; in scoped apps it is called as global.GlideQuery.
How do I run a data fix safely?
Try it on a sub-production instance first. Start with a dry run that only lists the records it would change, limit each run with setLimit() so a mistake stays small, and save the script as a Fix Script if the change must move with an update set. For deletes, check that no reference to those records has a cascade rule that deletes more than you expect.
Why should a business rule not call current.update()?
In a before rule, changes to current are saved anyway when the rule finishes. In an after rule, current.update() saves the record a second time, so every business rule runs again and can loop. Change current in a before rule, and use after or async rules to change other records, which is what the generator does.