Skip to content

Commit e026d5b

Browse files
authored
Comparing 2 records and show differences. (#2047)
* Add GlideReport for comparing 2 records * Update: Show backend and display values; add scope note in README * Update: Added system field check
1 parent d2e95cc commit e026d5b

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Compare Two Records Using GlideRecord (Global Scope)
2+
3+
This snippet compares two records from the same table in ServiceNow field-by-field using the **GlideRecord API**.
4+
It’s useful for debugging, verifying data after imports, or checking differences between two similar records.
5+
6+
---
7+
8+
## Working
9+
The script:
10+
1. Retrieves two records using their `sys_id`.
11+
2. Includes or Excludes the system fields.
12+
2. Iterates over all fields in the record.
13+
3. Logs any fields where the values differ.
14+
15+
---
16+
17+
## Scope
18+
This script is designed to run in the Global scope.
19+
If used in a scoped application, ensure that the target table is accessible from that scope (cross-scope access must be allowed).
20+
21+
## Usage
22+
Run this script in a **Background Script** or **Fix Script**:
23+
24+
```js
25+
compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here', false);
26+
```
27+
---
28+
## Example Output
29+
```js
30+
short_description: "Printer not working" vs "Printer offline"
31+
state: "In Progress" vs "Resolved"
32+
priority: "2" vs "3"
33+
Comparison complete.
34+
```
35+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Compare two records in a ServiceNow table field-by-field.
3+
* Logs all field differences between the two records, including display values.
4+
*
5+
* Parameters:
6+
* @param {string} table - Table name
7+
* @param {string} sys_id1 - sys_id of first record
8+
* @param {string} sys_id2 - sys_id of second record
9+
* @param {boolean} includeSystemFields - true to compare system fields, false to skip them
10+
*
11+
* Usage:
12+
* compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here', true/false);
13+
*/
14+
15+
function compareRecords(table, sys_id1, sys_id2, includeSystemFields) {
16+
var rec1 = new GlideRecord(table);
17+
var rec2 = new GlideRecord(table);
18+
19+
if (!rec1.get(sys_id1) || !rec2.get(sys_id2)) {
20+
gs.error('One or both sys_ids are invalid for table: ' + table);
21+
return;
22+
}
23+
24+
var fields = rec1.getFields();
25+
gs.info('Comparing records in table: ' + table);
26+
27+
for (var i = 0; i < fields.size(); i++) {
28+
var field = fields.get(i);
29+
var fieldName = field.getName();
30+
31+
if( !includeSystemFields && fieldName.startsWith('sys_') ) {
32+
continue;
33+
}
34+
35+
var val1 = rec1.getValue(fieldName);
36+
var val2 = rec2.getValue(fieldName);
37+
38+
var disp1 = rec1.getDisplayValue(fieldName);
39+
var disp2 = rec2.getDisplayValue(fieldName);
40+
41+
if (val1 != val2) {
42+
gs.info(
43+
fieldName + ': Backend -> "' + val1 + '" vs "' + val2 + '", ' +
44+
'Display -> "' + disp1 + '" vs "' + disp2 + '"'
45+
);
46+
}
47+
}
48+
49+
gs.info('Comparison complete.');
50+
}
51+
52+
// Example call
53+
compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here', false);

0 commit comments

Comments
 (0)