How to define maximum attachment size for one table

David Stutter
Tera Guru

The sys_property "com.glide.attachment.max_size" defines the global max size for attachments.

Is there any property or way to define specific tables which should have a bigger size limit?

For example in my case, I would like to have on the "sys_data_source" table a much bigger limit, but on the other hand, the incident should be limited.

4 REPLIES 4

Gurpreet07
Mega Sage

I have seen somewhere people been doing this using a before insert Business Rule on sys_attachment table. There is a size bytes field to check size and table_name to check record class.



if(current.size_bytes > 1000000 && current.table_name=='class_name'){


        current.setAbortAction(true);


}



One thing came into my mind is ... this table seems to be storing the record entry of the attachment but in actual attachment will reside somewhere in database. What if the record been created after attachment is uploaded.


I tried the business rule - it works just fine in my sandbox. I don't see any orphaned entries in sys_attachment or sys_attachment_doc.



I think the issue is that the user experience is poor. The business rule only validates upon an attempt to write the attachment, so the user has no way of knowing that their attachment exceeded the table-specific size limit. Adding a gs.addInfoMessage() message doesn't work either. So from the server-side, all you're going to get is a log message. I'm thinking you can probably send an email to the user, but I can't think of anything else more real-time than that.



To hack it up, I went to the attachments UI page, and made this change in the client script (lines 5-14 - from version Istanbul Patch 1):



function validateSizeandExt(field) {


  var form = $('sys_attachment');


  var maxSize = '';



  // Check the table name for specific attachment sizes


  if (g_form.getTableName() == 'incident')


  {


  maxSize = 1; // 1 MB


  }


  // Do the regular global size validation


  else


  {


  maxSize = (form.max_size && form.max_size.value) ? form.max_size.value : 0;


  }



  var fileTypes = (form.file_types && form.file_types.value) ? form.file_types.value : "";


  var files = field.files;


  var allowedSize = maxSize * 1048576;


              var warningString = "";


  for (var i = 0; i < files.length; i++) {


  if (files[i].size > allowedSize && allowedSize != 0)


  warningString += files[i].name + "${JS:gs.getMessage(' is ')}" + getDisplaySize(files[i].size) + "${JS:gs.getMessage('. The maximum file size is ')}" + getDisplaySize(allowedSize) + ".\n";


                              if (!isValidFileType(files[i], fileTypes))


                                              warningString += files[i].name + "${JS:gs.getMessage(' has a prohibited file extension.')}" + "\n";




  }


              if (warningString != "") {


                              alert(warningString);


                              clearFileField(field);


                              return 0;


              }




              return 1;


}



This should work for letting the user know of any issues with the file size for that table, but this is venturing off into customization land. Upgradeability may be impacted by this change.


I noticed later that this UI page is also used by email clients, where the g_form variable is not declared. To get around that, I also declared



var g_form = '';



and only executed the added code block "if (g_form)" was true.


Venkateswarlu K
Mega Guru

HI David



The following wiki article will show you how to find out the value in your instance - Administering Attachments - ServiceNow Wiki


It is a platform-wide limit and not based on application.In case you want to have a specific limit for any   you can write a business rule on sys_attachment table (Before Insert).



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


  if(parseInt(current.size_bytes) > 1000000 && current.table_name == 'incident')


{


//Abort the insert if size is greater than 1MB --> 1000000bytes


gs.log('Size has been exceeded');


current.setAbortAction(true);


}




})(current, previous);