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

16 REPLIES 16

@Raze00 

I informed to create Before Insert BR on sys_attachment

Condition: current.table_name == 'sn_hr_core_case'

Script: // add your logic and show error message

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

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

💡 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

Hi,

 

I tried this nothing happens the attachment gets uploaded and there is no error message.

 

thanks

Hi ankur , This works but i want to check that the combined size of all the attachments attached to it does not exceeds 18mb. Can you suggest how to do that?

 

Thanks

Pratham

@Raze00 

Glad to know that my script worked for you.

Simply enhance it like this

(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 + gr.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);

💡 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

I am trying with this script.

 

(function executeRule(current, previous) {

    var MAX_MB = 18;
    var MAX_BYTES = MAX_MB * 1024 * 1024;
    var totalSize = 0;

    // Calculate existing attachment size for the incident
    var ga = new GlideRecord('sys_attachment');
    ga.addQuery('table_name', current.table_name);
    ga.addQuery('table_sys_id', current.table_sys_id);
    ga.query();

    while(ga.next()){
        totalSize = totalSize + parseInt(ga.size_bytes,10);
    }

    // Block if total exceeds limit
    if (totalSize > MAX_BYTES){
        gs.addErrorMessage(
            'Total attachment size for this Incident cannot exceed 18 MB. Please remove some attachments.'
        );
        current.setAbortAction(true);
    }

})();