Purpose: How to write a GlideRecord loop to batch update Records in ServiceNow

Use: Use this script in a Server Script. Typically this will be used in a Background Script, Scheduled Job, or Fix Script in order to execute a batch operation to fix records, particularly after a data import, system upgrade, or app upgrade. In the following example, a fix could be applied to every incident record and a comment added to annotate the fix.

// This example will loop through all incident records and add a comment
var tableName = 'incident';
var incident = new GlideRecord(tableName);

incident.query();
while (incident.next()) {
    incident.comments = 'This comment will be added';
    incident.update();
}