Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

20 REPLIES 20

Hi, @Ankur Bawiskar  Tried flush message its not working. Can you think of a workaround for this like how we can achieve it?

 

@Raze00 

not very sure on that part.

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

Aditya_hublikar
Mega Sage

Hello @Raze00 ,

 

Yes this can be achieved using Before business rule . I Implemented this on incident table .

you can refer following code :

 
(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.sys_id);
    gr.addQuery('table_name', current.getTableName());
    gr.query();
    var size = 0;
    while (gr.next()) {
        size += gr.size_bytes;
    }


    if (size > 18* 1024 * 1024) {
        // greater than 18 MB
        gs.addErrorMessage('File size is greater than 18 MB');
        current.setAbortAction(true);
    } else {
        gs.addErrorMessage('File size is within limit');
        current.setAbortAction(false);
    }

    // Add your code here

})(current, previous);
   
adityahubli_0-1767696015702.png       adityahubli_1-1767696042508.png

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

 

 

will this trigger as soon as the attachment is added ? because i want the error to display as soon as the attachment is uploaded on any case.

Hello @Raze00 ,

Yes you can change code slightly.

Add before business rule on sys_attachment table

(function executeRule(current, previous /*null when async*/ ) {

if(current.table_name==your table name)
{
size=current.size_bytes.
if (size > 18* 1024 * 1024) {
// greater than 18 MB
gs.addErrorMessage('File size is greater than 18 MB');
current.setAbortAction(true);
} else {
gs.addErrorMessage('File size is within limit');
current.setAbortAction(false);
}
}
}

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant