diff --git a/Server-Side Components/Background Scripts/Get top 10 largest attachment/README.md b/Server-Side Components/Background Scripts/Get top 10 largest attachment/README.md new file mode 100644 index 0000000000..a03a0bc250 --- /dev/null +++ b/Server-Side Components/Background Scripts/Get top 10 largest attachment/README.md @@ -0,0 +1,19 @@ +# Top 10 Largest Attachments in ServiceNow + +This script retrieves and logs the top 10 largest file attachments from the `sys_attachment` table in ServiceNow. + +## Script Purpose + +- Find the largest attachments stored in the instance. +- Display file name, size in MB, and the related table. + +## How It Works + +- Queries the `sys_attachment` table. +- Sorts by `size_bytes` in descending order. +- Limits results to 10. +- Converts size to MB and logs details using `gs.info()`. + +## Usage + +Run this script in a **Background Script** or **Script Include** in ServiceNow. diff --git a/Server-Side Components/Background Scripts/Get top 10 largest attachment/top10AttachmentBySize.js b/Server-Side Components/Background Scripts/Get top 10 largest attachment/top10AttachmentBySize.js new file mode 100644 index 0000000000..536f90b028 --- /dev/null +++ b/Server-Side Components/Background Scripts/Get top 10 largest attachment/top10AttachmentBySize.js @@ -0,0 +1,27 @@ +// Create a GlideRecord object to query the 'sys_attachment' table +var grAttachments = new GlideRecord('sys_attachment'); + +// Sort the results by 'size_bytes' in descending order to get the largest attachments first +grAttachments.orderByDesc('size_bytes'); + +// Limit the query to return only the top 10 records +grAttachments.setLimit(10); + +// Execute the query +grAttachments.query(); + +// Log a header message to indicate the start of the output +gs.info('Top 10 largest attachments:'); + +// Loop through each record returned by the query +while (grAttachments.next()) { + // Convert the size from bytes to megabytes and round to 2 decimal places + var sizeMB = (grAttachments.size_bytes / (1024 * 1024)).toFixed(2); + + // Log the file name, size in MB, and the table the attachment belongs to + gs.info( + ' - ' + grAttachments.getDisplayValue('file_name') + + ' | Size: ' + sizeMB + ' MB' + + ' | Table: ' + grAttachments.getDisplayValue('table_name') + ); +}