The CreatorCon Call for Content is officially open! Get started here.

Business rule to reject small attachments (Less than 5KB)

michaelmorgan
Giga Contributor

I'm trying to build a business rule in order to reject small attachments (Our company has a few ones like the linkedin logo, twitter and the company one) which get attached on every incident. Is there a way I can set a business rule on the sys_attachment table which does this? I tried as you can see in the image below, but unfortunately it will not allow me to choose 'Less than' for 'Size bytes'. Does anyone know how I can set it to do this?

find_real_file.png

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Morgan,



you will have to write script for that. before insert business rule with following script.



var grInc = new GlideRecord('sys_attachment);


grInc.addQuery('table_name',"<tableName>"); // give table name as per your requirement


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>) { // give size in bytes as per your requirement


        gs.addInfoMessage("Attachment size should be at least <your Size Limit>");


        current.setAbortAction(true);


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Morgan,



you will have to write script for that. before insert business rule with following script.



var grInc = new GlideRecord('sys_attachment);


grInc.addQuery('table_name',"<tableName>"); // give table name as per your requirement


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>) { // give size in bytes as per your requirement


        gs.addInfoMessage("Attachment size should be at least <your Size Limit>");


        current.setAbortAction(true);


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



Thank you for your email, that is very helpful. I've just got one question:



Which table are you referring to here? Would this be the incident table in my example to prevent people inserting attachments for that table?



grInc.addQuery('table_name',"<tableName>"); // give table name as per your requirement


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



Thank you very much again.


Hi Morgan,



Consider user is attaching some attachment on incident record then in that case tableName would be incident.


If you have some other table then that name will come at that place.



Regards


Ankur


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

Jaspal Singh
Mega Patron
Mega Patron

Hi Michael,



You can make it run on update with below script which checks for size & aborts if it is less than 5 kb



if(current.size_bytes < 5'){


        current.setAbortAction(true);


}