diff --git a/Server-Side Components/Business Rules/Smart Attachment Size Limiter/Readme.md b/Server-Side Components/Business Rules/Smart Attachment Size Limiter/Readme.md new file mode 100644 index 0000000000..502c913352 --- /dev/null +++ b/Server-Side Components/Business Rules/Smart Attachment Size Limiter/Readme.md @@ -0,0 +1,7 @@ +Smart Attachment Size Limiter +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. + +Scoped Application Note: +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. + +This approach keeps your instance performant by managing attachment size transparently without hardcoded limits. diff --git a/Server-Side Components/Business Rules/Smart Attachment Size Limiter/Smart Attachment Size Limiter.js b/Server-Side Components/Business Rules/Smart Attachment Size Limiter/Smart Attachment Size Limiter.js new file mode 100644 index 0000000000..644e7b7b22 --- /dev/null +++ b/Server-Side Components/Business Rules/Smart Attachment Size Limiter/Smart Attachment Size Limiter.js @@ -0,0 +1,14 @@ +(function executeRule(current, previous /*null when async*/ ) { + if (current.table_name == 'incident') { //here multiple tables can be looped I am just using incident table + var maxSize = gs.getProperty('com.glide.attachment.max_size'); + maxSize = parseInt(maxSize, 10); + if (current.size_bytes > maxSize) { + var maxSizeMB = (maxSize / (1024 * 1024)).toFixed(2); + var attachmentSizeMB = (current.size_bytes / (1024 * 1024)).toFixed(2); + // Prevent insert by setting error message + gs.addErrorMessage("Attachment '" + current.file_name + "' size (" + attachmentSizeMB + " MB) exceeds the max allowed size of " + maxSizeMB + " MB. Please reduce the file size."); + // Cancel the insert operation + current.setAbortAction(true); + } + } +})(current, previous);