Total Attachment size limit

venkateshdhanap
Tera Contributor

Hi,

I have attachment limit size 20 MB.I have given this size limit in "System Properties>Security". But i can attach many files with 15 MB ,15 Mb..etc. How can i restrict total size of all attachments. For examble, if I am attaching   5 files for one Incident, total size of all those 5 files should not exceed 20 MB,How can calculate already attached files when attaching new file for though error?

!Thanks

1 ACCEPTED SOLUTION

manishm
Mega Guru

Hi Venkatesh,



i am not aware of a system property that would do that for you. You can however, write a script to restrict total size.   You can create a business rule before insert in sys_attachment table that checks the cumulative size of all attachments for a particular record. So lets say you are limiting size on the incident table. Something like:



var grInc = new GlideRecord('sys_attachment);


grInc.addQuery('table_name',"incident");


grInc.addQuery('table_sys_id',current.sys_id);


grInc.Query();


var totalSize=0;


while(grInc.next()) {


        totalSize+= parseInt(grInc.size_bytes);


}


if(totalSize+parseInt(current.size_bytes) > <your Size Limit>) {


        gs.addInfoMessage("Attachment size exceeded");


        current.setAbortAction(true);


}



Manish



PS: Mark as Helpful or Correct if this helps or solves your problem


View solution in original post

11 REPLIES 11

Kalaiarasan Pus
Giga Sage

I am not sure if there is a out of the box property is present for this. But if it not, you can try adding a before insert business rule on sys_attachment table.



  • In your business rule, query the attachments that are related to the current record. You can get the ID of the current record using Table sys ID column.
  • Loop the existing records and add up the Size bytes column to get the cumulative size of all the attachments.
  • Abort the attachment insertion using current.setAbortAction(true) if it crosses your threshold limit.

Chuck Tomasi
Tera Patron

Hi Venkatesh,



While you cannot set a property to do this, you could write yourself an after insert business rule that could do the checking.



You need to do a GlideAggregate operation on the sys_attachment table using the SUM operator on the size_bytes field for all records where table=your table name, and table_sys_id=your table record.



If the sum comes back > 20MB, throw an error (and optionally delete the recently attached file from the sys_attachment table.)



Reference:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


GlideAggregate - ServiceNow Wiki


My bad. I forgot about about the sum operator that is OOB.


Harsh Vardhan
Giga Patron

Hi Venkatesh,



Please check the thread below. hope it will help you



How to define maximum attachment size for one table



Thanks,


Harshvardhan