diff --git a/Core ServiceNow APIs/GlideRecord/Field Level Audit/README.md b/Core ServiceNow APIs/GlideRecord/Field Level Audit/README.md new file mode 100644 index 0000000000..3369d89e5a --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Field Level Audit/README.md @@ -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' +``` \ No newline at end of file diff --git a/Core ServiceNow APIs/GlideRecord/Field Level Audit/fieldLevelAudit.js b/Core ServiceNow APIs/GlideRecord/Field Level Audit/fieldLevelAudit.js new file mode 100644 index 0000000000..5020556293 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Field Level Audit/fieldLevelAudit.js @@ -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); + } + }); +}