From 517e1ff928fab90247b735a69680943bfac8ddb6 Mon Sep 17 00:00:00 2001 From: Rajasree2004 Date: Sun, 12 Oct 2025 13:26:51 +0530 Subject: [PATCH 1/3] Add GlideReport for comparing 2 records --- .../GlideRecord/Compare_2_records/README.md | 29 +++++++++++++++ .../Compare_2_records/compareRecords.js | 36 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md create mode 100644 Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js diff --git a/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md b/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md new file mode 100644 index 0000000000..832f19e82b --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md @@ -0,0 +1,29 @@ +# Compare Two Records Using GlideRecord + +This snippet compares two records from the same table in ServiceNow field-by-field using the **GlideRecord API**. +It’s useful for debugging, verifying data after imports, or checking differences between two similar records. + +--- + +## Working +The script: +1. Retrieves two records using their `sys_id`. +2. Iterates over all fields in the record. +3. Logs any fields where the values differ. + +--- + +## Usage +Run this script in a **Background Script** or **Fix Script**: + +```js +compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); +``` + +## Example Output +```js +short_description: "Printer not working" vs "Printer offline" +state: "In Progress" vs "Resolved" +priority: "2" vs "3" +Comparison complete. +``` \ No newline at end of file diff --git a/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js b/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js new file mode 100644 index 0000000000..c45dbc608d --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js @@ -0,0 +1,36 @@ +/** + * Compare two records in a ServiceNow table field-by-field. + * Logs all field differences between the two records. + * + * Usage: + * compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); + */ + +function compareRecords(table, sys_id1, sys_id2) { + var rec1 = new GlideRecord(table); + var rec2 = new GlideRecord(table); + + if (!rec1.get(sys_id1) || !rec2.get(sys_id2)) { + gs.error('One or both sys_ids are invalid for table: ' + table); + return; + } + + var fields = rec1.getFields(); + gs.info('Comparing records in table: ' + table); + + for (var i = 0; i < fields.size(); i++) { + var field = fields.get(i); + var fieldName = field.getName(); + var val1 = rec1.getValue(fieldName); + var val2 = rec2.getValue(fieldName); + + if (val1 != val2) { + gs.info(fieldName + ': "' + val1 + '" vs "' + val2 + '"'); + } + } + + gs.info('Comparison complete.'); +} + +// Example call +compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); From 2b1aa128ead00a5a2a7d99c84aaa1e718ead6a76 Mon Sep 17 00:00:00 2001 From: Rajasree2004 Date: Sun, 12 Oct 2025 14:16:15 +0530 Subject: [PATCH 2/3] Update: Show backend and display values; add scope note in README --- .../GlideRecord/Compare_2_records/README.md | 11 ++++++++--- .../GlideRecord/Compare_2_records/compareRecords.js | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md b/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md index 832f19e82b..1ce32a63d0 100644 --- a/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md +++ b/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md @@ -1,4 +1,4 @@ -# Compare Two Records Using GlideRecord +# Compare Two Records Using GlideRecord (Global Scope) This snippet compares two records from the same table in ServiceNow field-by-field using the **GlideRecord API**. It’s useful for debugging, verifying data after imports, or checking differences between two similar records. @@ -13,17 +13,22 @@ The script: --- +## Scope +This script is designed to run in the Global scope. +If used in a scoped application, ensure that the target table is accessible from that scope (cross-scope access must be allowed). + ## Usage Run this script in a **Background Script** or **Fix Script**: ```js compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); ``` - +--- ## Example Output ```js short_description: "Printer not working" vs "Printer offline" state: "In Progress" vs "Resolved" priority: "2" vs "3" Comparison complete. -``` \ No newline at end of file +``` + diff --git a/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js b/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js index c45dbc608d..93cf6fb627 100644 --- a/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js +++ b/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js @@ -1,6 +1,6 @@ /** * Compare two records in a ServiceNow table field-by-field. - * Logs all field differences between the two records. + * Logs all field differences between the two records, including display values. * * Usage: * compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); @@ -21,11 +21,18 @@ function compareRecords(table, sys_id1, sys_id2) { for (var i = 0; i < fields.size(); i++) { var field = fields.get(i); var fieldName = field.getName(); + var val1 = rec1.getValue(fieldName); var val2 = rec2.getValue(fieldName); + var disp1 = rec1.getDisplayValue(fieldName); + var disp2 = rec2.getDisplayValue(fieldName); + if (val1 != val2) { - gs.info(fieldName + ': "' + val1 + '" vs "' + val2 + '"'); + gs.info( + fieldName + ': Backend -> "' + val1 + '" vs "' + val2 + '", ' + + 'Display -> "' + disp1 + '" vs "' + disp2 + '"' + ); } } From bcd0a9258aa13972297b206d966e6ccf40c92874 Mon Sep 17 00:00:00 2001 From: Rajasree2004 Date: Sun, 12 Oct 2025 15:14:37 +0530 Subject: [PATCH 3/3] Update: Added system field check --- .../GlideRecord/Compare_2_records/README.md | 3 ++- .../Compare_2_records/compareRecords.js | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md b/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md index 1ce32a63d0..07367d9014 100644 --- a/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md +++ b/Core ServiceNow APIs/GlideRecord/Compare_2_records/README.md @@ -8,6 +8,7 @@ It’s useful for debugging, verifying data after imports, or checking differenc ## Working The script: 1. Retrieves two records using their `sys_id`. +2. Includes or Excludes the system fields. 2. Iterates over all fields in the record. 3. Logs any fields where the values differ. @@ -21,7 +22,7 @@ If used in a scoped application, ensure that the target table is accessible from Run this script in a **Background Script** or **Fix Script**: ```js -compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); +compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here', false); ``` --- ## Example Output diff --git a/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js b/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js index 93cf6fb627..2988299c0d 100644 --- a/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js +++ b/Core ServiceNow APIs/GlideRecord/Compare_2_records/compareRecords.js @@ -1,12 +1,18 @@ /** * Compare two records in a ServiceNow table field-by-field. * Logs all field differences between the two records, including display values. - * + * + * Parameters: + * @param {string} table - Table name + * @param {string} sys_id1 - sys_id of first record + * @param {string} sys_id2 - sys_id of second record + * @param {boolean} includeSystemFields - true to compare system fields, false to skip them + * * Usage: - * compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); + * compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here', true/false); */ -function compareRecords(table, sys_id1, sys_id2) { +function compareRecords(table, sys_id1, sys_id2, includeSystemFields) { var rec1 = new GlideRecord(table); var rec2 = new GlideRecord(table); @@ -21,6 +27,10 @@ function compareRecords(table, sys_id1, sys_id2) { for (var i = 0; i < fields.size(); i++) { var field = fields.get(i); var fieldName = field.getName(); + + if( !includeSystemFields && fieldName.startsWith('sys_') ) { + continue; + } var val1 = rec1.getValue(fieldName); var val2 = rec2.getValue(fieldName); @@ -40,4 +50,4 @@ function compareRecords(table, sys_id1, sys_id2) { } // Example call -compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here'); +compareRecords('incident', 'sys_id_1_here', 'sys_id_2_here', false);