File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed
Core ServiceNow APIs/GlideRecord/Compare_2_records Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ # Compare Two Records Using GlideRecord
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 . Iterates over all fields in the record.
12+ 3 . Logs any fields where the values differ.
13+
14+ ---
15+
16+ ## Usage
17+ Run this script in a ** Background Script** or ** Fix Script** :
18+
19+ ``` js
20+ compareRecords (' incident' , ' sys_id_1_here' , ' sys_id_2_here' );
21+ ```
22+
23+ ## Example Output
24+ ``` js
25+ short_description: " Printer not working" vs " Printer offline"
26+ state: " In Progress" vs " Resolved"
27+ priority: " 2" vs " 3"
28+ Comparison complete.
29+ ```
Original file line number Diff line number Diff line change 1+ /**
2+ * Compare two records in a ServiceNow table field-by-field.
3+ * Logs all field differences between the two records.
4+ *
5+ * Usage:
6+ * compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here');
7+ */
8+
9+ function compareRecords ( table , sys_id1 , sys_id2 ) {
10+ var rec1 = new GlideRecord ( table ) ;
11+ var rec2 = new GlideRecord ( table ) ;
12+
13+ if ( ! rec1 . get ( sys_id1 ) || ! rec2 . get ( sys_id2 ) ) {
14+ gs . error ( 'One or both sys_ids are invalid for table: ' + table ) ;
15+ return ;
16+ }
17+
18+ var fields = rec1 . getFields ( ) ;
19+ gs . info ( 'Comparing records in table: ' + table ) ;
20+
21+ for ( var i = 0 ; i < fields . size ( ) ; i ++ ) {
22+ var field = fields . get ( i ) ;
23+ var fieldName = field . getName ( ) ;
24+ var val1 = rec1 . getValue ( fieldName ) ;
25+ var val2 = rec2 . getValue ( fieldName ) ;
26+
27+ if ( val1 != val2 ) {
28+ gs . info ( fieldName + ': "' + val1 + '" vs "' + val2 + '"' ) ;
29+ }
30+ }
31+
32+ gs . info ( 'Comparison complete.' ) ;
33+ }
34+
35+ // Example call
36+ compareRecords ( 'incident' , 'sys_id_1_here' , 'sys_id_2_here' ) ;
You can’t perform that action at this time.
0 commit comments