Strip Email Attachments Inbound Action

wmahmud1
Kilo Explorer

Hello -

I am trying to strip all attachments that get picked up by ServiceNow under 5000 bytes so we can avoid unecessary attachments of users signatures to records.

I have looked at some examples posted already and setup something similar.   My issue is, the business rule on the sys_attachment table seems to run as I can see the script log showing the name of the file being aborted, but the email log itself, is not capturing the target (remains as empty) and not communicating back to the record.   If i turn off this busienss rule, it works fine again.

Any ideas what the issue may be?

(see attached business rule detail)

28 REPLIES 28

Carl Fransen1
Tera Guru

Hi,

I have been using the below for months in production and it works a treat - we use this to remove all the email signature logo's (we have over 9 on our standard signature).

Created a Business rule - ADVANCED, BEFORE, INSERT

Condition:  parseInt(current.size_bytes) <= 5000

Script:

//  code added to check email attachments and remove if the size is equal or less than that specified in the condition
//  CJF 5/4/17 - Post MVP Incident Go-Live
//  CJF 17/4/18 - Updated to remove new HR Email Banner pictures from new templates

(function executeRule(current, previous /*null when async*/) {
 
  var email_log = new GlideRecord('syslog');
  var email = new GlideRecord('sys_email');
  email.addQuery('instance',current.table_sys_id);
  email.orderByDesc('sys_created_on');
  email.query();
  var email_sys_id ='';
  var email_text = '';
  var imagename = current.file_name;
  if(email.next())
      {
      email_sys_id = email.sys_id;
  }
  email_text = 'EMAIL'+'.'+email_sys_id;
  email_log.addQuery('source','CONTAINS', email_text);
  email_log.addQuery('message','CONTAINS',imagename);
  email_log.query();
  if(email_log.next())
  {
     current.setAbortAction(true);
  }
 
})(current, previous);

 

I hope this helps

Cheers Carl.

 

...please mark correct or helpful where appropriate

Hi Carl,

 

I found this works great, however, we have customers that send thing in text documents and in some cases are under the size of the signature attachments. Any thoughts on how to suppress the sig attachments but allow others through?

 

thanks.

We don't have smaller files being attached so it's not an issue for us.  When we tested our code initially we used things like a blank word document - which was 19KB, well over the 5000 bytes on our condition, and we did the same testing with images and other file types. We also looked in the images inside ServiceNow to determine what is being sent and our 5000 bytes covered all of our requirements.

You could look at saving all your signature files to images, finding the exact size as I mentioned to Brian in another one of my posts and then specifically excluding these in your condition - as below:

parseInt(current.size_bytes) <= 5000 || parseInt(current.size_bytes) == 27591 || parseInt(current.size_bytes) == 29272 || parseInt(current.size_bytes) == 29251

What I also found was that the size was the 'size on disk', not the 'file size' when you right-click and look at the properties of the file to gain the exact size.

 

Hope this helps.

Carl.

Is there any alternative logic to use instead of this addquery  

 email.addQuery('instance',current.table_sys_id);

Hello - unfortunately I'm not aware of any alternative logic, but then again my development skills are average at best.   It might be worth asking this as a separate question on the developers area in the community - someone might know the answer to this for you.

 

Cheers

Carl.