Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Core ServiceNow APIs/GlideRecord/Field Level Audit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# GlideRecord Field-Level Audit

## Description
This snippet compares two GlideRecord objects field by field and logs all differences.
It is useful for debugging, auditing updates, or validating changes in Business Rules, Script Includes, or Background Scripts.

## Prerequisites
- Server-side context (Background Script, Business Rule, Script Include)
- Two GlideRecord objects representing the original and updated records
- Access to the table(s) involved

## Note
- Works in Global Scope
- Server-side execution only
- Logs all fields with differences to system logs
- Does not modify any records
## Usage
```javascript
// Load original record
var oldRec = new GlideRecord('incident');
oldRec.get('sys_id_here');

// Load updated record
var newRec = new GlideRecord('incident');
newRec.get('sys_id_here');

// Compare and log differences
fieldLevelAudit(oldRec, newRec);
```

## Output
```
Field changed: priority | Old: 5 | New: 2
Field changed: state | Old: 1 | New: 3
Field changed: short_description | Old: 'Old description' | New: 'New description'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Compare two GlideRecord objects field by field and log differences.
*
* @param {GlideRecord} grOld - Original record before changes
* @param {GlideRecord} grNew - Updated record to compare against
*/
function fieldLevelAudit(grOld, grNew) {
if (!grOld || !grNew) {
gs.error('Both old and new GlideRecord objects are required.');
return;
}

var fields = grOld.getFields();
fields.forEach(function(f) {
var name = f.getName();
var oldValue = grOld.getValue(name);
var newValue = grNew.getValue(name);

if (oldValue != newValue) {
gs.info('Field changed: ' + name +
' | Old: ' + oldValue +
' | New: ' + newValue);
}
});
}
Loading