Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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')
);
}
Loading