GlideRecord reads and writes records on the server. Build the query, run it, then loop over the results. It ignores ACLs, so use GlideRecordSecure when the data goes back to a user.
Returns true when there is at least one more record, without moving to it.
var gr = newGlideRecord('sys_user_group');
gr.addQuery('name', 'Service Desk');
gr.setLimit(1);
gr.query();
if (gr.hasNext()) {
gs.info('Group exists');
}
initialize() prepares a new record with default values; insert() saves it and returns its sys_id.
var inc = newGlideRecord('incident');
inc.initialize();
inc.setValue('short_description', 'Printer on floor 3 is offline');
inc.setValue('caller_id', gs.getUserID());
var incidentId = inc.insert();
Without initialize() (or newRecord()), field defaults are not applied.
Skips business rules, notifications, workflows and audit for the next update.
var inc = newGlideRecord('incident');
if (inc.get(incidentId)) {
inc.setWorkflow(false); // data fix: skip notifications on purpose
inc.setValue('u_migrated', true);
inc.update();
}
Use it only when skipping all of that is intended, such as data fixes.
Updates the record without changing Updated, Updated by and the update count.
var inc = newGlideRecord('incident');
if (inc.get(incidentId)) {
inc.autoSysFields(false); // data fix: keep the original updated date
inc.setValue('u_migrated', true);
inc.update();
}
var inc = newGlideRecord('incident');
inc.addActiveQuery();
inc.query();
var count = inc.getRowCount();
It loads every matching record just to count them. Use GlideAggregate with COUNT instead.
Frequently asked questions
What is the difference between getValue() and dot-walking a field?
gr.getValue('state') returns the value as a string. gr.state returns a GlideElement object, which behaves like a string in most places but breaks strict comparisons (===) and stores a reference when pushed into an array inside a loop.
When should I use GlideRecordSecure instead of GlideRecord?
Whenever the records go back to a user: client-callable script includes (GlideAjax), scripted REST APIs and widgets. GlideRecordSecure applies ACLs; GlideRecord does not.
Why does my GlideRecord query return every record?
ServiceNow ignores conditions on field names it does not recognise, so a typo such as prority matches everything. Check the field name, or set the system property glide.invalid_query.returns_no_rows to true.