attachment size on workspace

Raze00
Tera Contributor

Hi i want to display the message as soon as possible whenever an attachment is attached to the case and the combine size of attachments exceed 18mb then i want to show the error message immediately is this possible?

 

1 ACCEPTED SOLUTION

@Raze00 

Small change in my Script & it worked fine for me

-> I added size bytes for the current file also, I forgot it earlier

(function executeRule(current, previous /*null when async*/ ) {
    // Define your limit in bytes (e.g., 10MB)
    var maxSize = 18 * 1024 * 1024; // 18 MB

    var totalSize = 0;
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_sys_id", current.table_sys_id);
    gr.query();
    while (gr.next()) {
        totalSize = totalSize + parseInt(gr.size_bytes);
    }

    totalSize = totalSize + parseInt(current.size_bytes);

    if (totalSize > maxSize) {
        gs.addErrorMessage('Total Attachment size exceeds the limit of 18 MB.'); // User-friendly message
        current.setAbortAction(true); // Prevents the attachment from being saved
    }
})(current, previous);

AnkurBawiskar_1-1767878457088.png

 

AnkurBawiskar_0-1767878445597.png

 

Output: I tried to add 3rd file and it exceeded 18MB and error message thrown and file not added

attachment size combine 18MB.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

18 REPLIES 18

Raze00
Tera Contributor

But this is not getting triggered only the attachments gets uploaded without any error and abort.

 

@Raze00 

Small change in my Script & it worked fine for me

-> I added size bytes for the current file also, I forgot it earlier

(function executeRule(current, previous /*null when async*/ ) {
    // Define your limit in bytes (e.g., 10MB)
    var maxSize = 18 * 1024 * 1024; // 18 MB

    var totalSize = 0;
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_sys_id", current.table_sys_id);
    gr.query();
    while (gr.next()) {
        totalSize = totalSize + parseInt(gr.size_bytes);
    }

    totalSize = totalSize + parseInt(current.size_bytes);

    if (totalSize > maxSize) {
        gs.addErrorMessage('Total Attachment size exceeds the limit of 18 MB.'); // User-friendly message
        current.setAbortAction(true); // Prevents the attachment from being saved
    }
})(current, previous);

AnkurBawiskar_1-1767878457088.png

 

AnkurBawiskar_0-1767878445597.png

 

Output: I tried to add 3rd file and it exceeded 18MB and error message thrown and file not added

attachment size combine 18MB.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thankyou ankur for your reply. I did this yesterday and yeah its kinda same of what you suggested above and it works fine.

 

(function executeRule(current, previous /* null when async */) {
    // Max combined size across all attachments on the same record
    var MAX_TOTAL_BYTES = 18 * 1024 * 1024; // 18 MB

    var totalBytes = 0;

    // Sum sizes of all existing attachments on the same target record,
    // excluding the current record to avoid double-counting.
    var attGR = new GlideRecord('sys_attachment');
    attGR.addQuery('table_name', current.getValue('table_name'));
    attGR.addQuery('table_sys_id', current.getValue('table_sys_id'));
    attGR.addQuery('sys_id', '!=', current.getUniqueValue());
    attGR.query();

    while (attGR.next()) {
        var sz = parseInt(attGR.getValue('size_bytes'), 10);
        if (!isNaN(sz)) {
            totalBytes += sz;
        }
    }

    // Include the size of the attachment being added/updated
    var currentSize = parseInt(current.getValue('size_bytes'), 10);
    if (!isNaN(currentSize)) {
        totalBytes += currentSize;
    }

    // If combined size exceeds the limit, prevent save and show a clear message
    if (totalBytes > MAX_TOTAL_BYTES) {
        var limitMB = (MAX_TOTAL_BYTES / (1024 * 1024));                  // 18
        var totalMB = (totalBytes / (1024 * 1024)).toFixed(2);            // e.g., 18.73
        gs.addErrorMessage('Total attachment size (' + totalMB + ' MB) exceeds the limit of ' + limitMB + ' MB.');
    }
})(current, previous);
``

Hi @Ankur Bawiskar is it possible like if we are uploading multiple attachments simultaneously then it will spam this error message for multiple times. Is it possible that we can clear the previous message and only displaye the last error message while uploading multiple attachments? so that for the user it will not spam error messages simultaneously.

 

@Raze00 

try to use below, I am not sure if that works

gs.flushMessages();
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader