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

stevedag
Kilo Expert

Nobody had problem with attachment filename limit? Since I had no answer in the community, I did open a service ticket in HI-ServiceNow.   I let you know if I ever get a solution to the filename extension lost (truncation).


Jon23
Mega Sage

Hi Steve D'Agostino,



I have the same issue and a little reluctant to modify the dictionary entry for the file_name on the sys_attachment table.



So, I created a business rule on the sys_attachment table to run before an Insert or Update.



Condition:


current.file_name.toString().length > 100



Script:


truncateFilename();



function truncateFilename() {



// maximum file_name length including extension


    var maxLength = 100;



// Split out the filename based on . so we can get the file extension


    var fnParts = current.file_name.split('.');



// Cycle through the filename parts to get the extension


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


              fnExt = fnParts[i];


    }



//Get the length of the last part +2 to allow for ~.


  var extLength = fnExt.length + 2;



//Shorten the filename and keep the extension


  current.file_name = current.file_name.substring(0, (maxLength - extLength)) + '~.' + fnExt;



}



My testing is giving me the results I need (not moved to production yet), hopefully it will help you



Scripting is not my strong point so let me know if there are potential issues, or ways to improve this script.


Moved the business rule to test instance and it no longer works



Before creating the business rule in Dev I had modified the file_name field to 255 characters. This appears to be the key factor for why my script working.



My test instance still has the 100 character limit - If i increase the field size I don't need the script!



Still reluctant to increase the field size in production - what are the risks in doing this?



Steve D'Agostino, anything from HI ticket?


Hi jwalton,



I'm sorry to read that it doesn't work in your test instance.   I wanted to try your solution this afternoon in our DEV instance.   I'll probably give it a try anyways.   It's a good idea, let's see if we can make it work.



There's no news from Hi in the ticket.   No solution found yet.