Attachment loses its file extension when length of the filename exceeds the limit

stevedag
Kilo Expert

Is there a way to keep the file extension when a user puts an attachment with a filename that exceeds the limit set in ServiceNow?

 

I know we can change the maximum lenght of the filename in the sys_attachment table, but that's not what I want.     I would like to cut the filename to the default 100 caracters limit, but to keep its file extension.   This way, although the filename has been truncated, our users will still be able to open it.


Thanks!

Steve

23 REPLIES 23

Thanks a lot


Introduction to Fields - ServiceNow Wiki


For 254 characters or less, the string field will be a single-line text field. Anything 255 characters or over will appear as a multi-line text box.


Note


Note: The database may translate the value you provide in the max_length field to the closest matching database field type. For example, a max string length of 80 maps to the nearest database field type of VARCHAR(100). See Database Field Types for common database field types.


If it still doesn't work you may need to open a ticket in HI based on this thread: Strings shortened despite increasing max length of field


wamitlehner
Kilo Explorer

Hope this helps:



As you've found out already, by the time the filename is passed to the business rule, it has already been truncated so any manipulation at that point is too late.



However, it is possible to add an additional validation to the client script section of the attachment UI Page.   This is single if statement that checks the length of the filename as shown below:



function validateSizeandExt(field) {


var form = $('sys_attachment');


var 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";



// validate filename does not exceed 100


  if (files[i].name.length > 100)


  warningString += files[i].name + "${JS:gs.getMessage(' filename length is greater than 100.')}"


                           


}


              if (warningString != "") {


                              alert(warningString);


                              clearFileField(field);


                              return 0;


              }


           


              return 1;


}


Snow Angel
Tera Expert

@stevedag @ryadavalli 

Was there an accepted solution for this query "Attachment loses its file extension when length of the filename exceeds the limit"?