diff --git a/Core ServiceNow APIs/GlideRecord/Order By Field/README.md b/Core ServiceNow APIs/GlideRecord/Order By Field/README.md new file mode 100644 index 0000000000..0c6f324bb2 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Order By Field/README.md @@ -0,0 +1,12 @@ +# Order By Scripts + +This folder contains two JavaScript files that demonstrate sorting functionality. + +## Files + +- **orderByAscending.js** – Sorts data in ascending order. +- **orderByDescending.js** – Sorts data in descending order. + +## Usage + +Include either script in your file and call the respective function to sort your table as needed. diff --git a/Core ServiceNow APIs/GlideRecord/Order By Field/orderByAscending.js b/Core ServiceNow APIs/GlideRecord/Order By Field/orderByAscending.js new file mode 100644 index 0000000000..aa65af974f --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Order By Field/orderByAscending.js @@ -0,0 +1,6 @@ +function ascOrder(tableName, field) { + var tableGr = new GlideRecord(tableName); + tableGr.addActiveQuery(); + tableGr.orderBy(field); + tableGr.query(); +} \ No newline at end of file diff --git a/Core ServiceNow APIs/GlideRecord/Order By Field/orderByDescending.js b/Core ServiceNow APIs/GlideRecord/Order By Field/orderByDescending.js new file mode 100644 index 0000000000..07b63f7802 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Order By Field/orderByDescending.js @@ -0,0 +1,6 @@ +function ascOrder(tableName, field) { + var tableGr = new GlideRecord(tableName); + tableGr.addActiveQuery(); + tableGr.orderByDesc(field); + tableGr.query(); +} \ No newline at end of file diff --git a/Duplicate Attachment Finder/README.md b/Duplicate Attachment Finder/README.md new file mode 100644 index 0000000000..a904bb4458 --- /dev/null +++ b/Duplicate Attachment Finder/README.md @@ -0,0 +1,9 @@ +# Duplicate Attachment Finder + +This script identifies duplicate attachments in a ServiceNow instance based on file name and size, and lists them in descending order of creation date. + +## Features + +- Finds attachments with the same **file name** and **size**. +- Orders duplicates by **creation date (latest first)**. +- Shows the **attachment Sys ID**, **parent table**, and **record**. \ No newline at end of file diff --git a/Duplicate Attachment Finder/duplicateAttachments.js b/Duplicate Attachment Finder/duplicateAttachments.js new file mode 100644 index 0000000000..8608ddd821 --- /dev/null +++ b/Duplicate Attachment Finder/duplicateAttachments.js @@ -0,0 +1,43 @@ +(function() { + var attachmentGr = new GlideRecord('sys_attachment'); + attachmentGr.orderByDesc('sys_created_on'); + attachmentGr.query(); + + var attachments = {}; + var duplicateList = []; + + while(attachmentGr.next()) { + var key = attachmentGr.file_name + '|' + attachmentGr.size_bytes; + if(!attachments[key]) { + attachments[key] = []; + } + attachmentGr[key].push({ + sys_id: attachmentGr.sys_id.toString(), + table_name: attachmentGr.table_name.toString(), + table_sys_id: attachmentGr.table_sys_id.toString, + created_on: attachmentGr.sys_created_on.toString() + }) + } + + for (var i in attachments) { + if(attachments[i].length > 1) { + duplicateList.push({ + file_key: i, + count: attachments[i].length, + records: attachments[i] + }) + } + } + + if (duplicateList.length === 0) { + gs.info('No duplicate attachments found.'); + } else { + gs.info('Found ' + duplicateList.length + ' duplicate attachments:'); + duplicateList.forEach(function(dup) { + gs.info('File: ' + dup.file_key + ' | Count: ' + dup.count); + dup.records.forEach(function(r) { + gs.info(' Sys_ID: ' + r.sys_id + ' | Table: ' + r.table_name + ' | Record: ' + r.table_sys_id + ' | Created On: ' + r.created_on); + }); + }); + } +}()); \ No newline at end of file