Skip to content

Commit e347eb4

Browse files
committed
Add Field Level Audit
1 parent 93891d6 commit e347eb4

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# GlideRecord Field-Level Audit
2+
3+
## Description
4+
This snippet compares two GlideRecord objects field by field and logs all differences.
5+
It is useful for debugging, auditing updates, or validating changes in Business Rules, Script Includes, or Background Scripts.
6+
7+
## Prerequisites
8+
- Server-side context (Background Script, Business Rule, Script Include)
9+
- Two GlideRecord objects representing the original and updated records
10+
- Access to the table(s) involved
11+
12+
## Note
13+
- Works in Global Scope
14+
- Server-side execution only
15+
- Logs all fields with differences to system logs
16+
- Does not modify any records
17+
## Usage
18+
```javascript
19+
// Load original record
20+
var oldRec = new GlideRecord('incident');
21+
oldRec.get('sys_id_here');
22+
23+
// Load updated record
24+
var newRec = new GlideRecord('incident');
25+
newRec.get('sys_id_here');
26+
27+
// Compare and log differences
28+
fieldLevelAudit(oldRec, newRec);
29+
```
30+
31+
## Output
32+
```
33+
Field changed: priority | Old: 5 | New: 2
34+
Field changed: state | Old: 1 | New: 3
35+
Field changed: short_description | Old: 'Old description' | New: 'New description'
36+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Compare two GlideRecord objects field by field and log differences.
3+
*
4+
* @param {GlideRecord} grOld - Original record before changes
5+
* @param {GlideRecord} grNew - Updated record to compare against
6+
*/
7+
function fieldLevelAudit(grOld, grNew) {
8+
if (!grOld || !grNew) {
9+
gs.error('Both old and new GlideRecord objects are required.');
10+
return;
11+
}
12+
13+
var fields = grOld.getFields();
14+
fields.forEach(function(f) {
15+
var name = f.getName();
16+
var oldValue = grOld.getValue(name);
17+
var newValue = grNew.getValue(name);
18+
19+
if (oldValue != newValue) {
20+
gs.info('Field changed: ' + name +
21+
' | Old: ' + oldValue +
22+
' | New: ' + newValue);
23+
}
24+
});
25+
}

0 commit comments

Comments
 (0)