Skip to content

Commit 2db414f

Browse files
authored
Smart Attachment Size Limiter Business Rule (#2162)
* Create Readme.md * Create Smart Attachment Size Limiter.js * Delete Server-Side Components/Business Rules/Smart Attachment Size Limiter directory * Create Smart Attachment Size Limiter.js * Create Readme.md
1 parent 9f68497 commit 2db414f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Smart Attachment Size Limiter
2+
This Business Rule limits attachment sizes uploaded to ServiceNow records by enforcing a maximum size configured via the system property com.glide.attachment.max_size (in bytes). If the attachment exceeds the configured limit, the upload is blocked with an error message shown to the user. You can create or modify this system property to change the max size and update the property name in the script accordingly.
3+
4+
Scoped Application Note:
5+
If deploying in a scoped application, configure Cross-Scope Access under System Applications > Application Cross-Scope Access to allow your app permission to access the sys_attachment table and related resources, avoiding security restrictions.
6+
7+
This approach keeps your instance performant by managing attachment size transparently without hardcoded limits.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(function executeRule(current, previous /*null when async*/ ) {
2+
if (current.table_name == 'incident') { //here multiple tables can be looped I am just using incident table
3+
var maxSize = gs.getProperty('com.glide.attachment.max_size');
4+
maxSize = parseInt(maxSize, 10);
5+
if (current.size_bytes > maxSize) {
6+
var maxSizeMB = (maxSize / (1024 * 1024)).toFixed(2);
7+
var attachmentSizeMB = (current.size_bytes / (1024 * 1024)).toFixed(2);
8+
// Prevent insert by setting error message
9+
gs.addErrorMessage("Attachment '" + current.file_name + "' size (" + attachmentSizeMB + " MB) exceeds the max allowed size of " + maxSizeMB + " MB. Please reduce the file size.");
10+
// Cancel the insert operation
11+
current.setAbortAction(true);
12+
}
13+
}
14+
})(current, previous);

0 commit comments

Comments
 (0)