How to filter out attachments from email and update work-notes with the email body?

naam
Giga Contributor

This is the inbound email action we have right now. I want to copy only the email body to the inbound email so it gets updated to worknotes and avoid attachments. Evern better if i could filter out anything below the watermark from the email.

gs.include('validators');

if (current.getTableName() == "sc_req_item") {
current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;

/*
if (gs.hasRole("itil")) {
if (email.body.assign != undefined)
current.assigned_to = email.body.assign;

if (email.body.priority != undefined && isNumeric(email.body.priority))
current.priority = email.body.priority;
}
*/
current.update();
}

2 REPLIES 2

Nikhil Dixit
Giga Expert

Hi,

 

Please look into below, I believe this will help you. Please mark answer correct and helpful.

To Avoid attachments !!!!!!!!!!!!

I have found a easier way of doing this.
I have created a business rule on sys_attachment table

When -> After
--Insert
--Update

Table Name = 'table name'

Advanced

Script that does the check and deletion of the attachment record


Logic Used :

syslog table captures details of the EMAIL record that has created the attachment and the name of the attachment.
sys_email table captures details of the in bound email and the target(instance) record reference.
I am trying to find out the latest email record that has created the attachment, in order to get that I run a glide record on sys_email where target(instance) = table sys id field from sys_attachment table
Once I get the sys id of the sys_email record, I build the source text which follows below convention.

EMAIL.sys_id

Message contains imagename.

if the syslog matches the email record for the attachment record table sys id, then attachment will be deleted.

################CODE START#########################

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

       // Add your code here

       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.deleteRecord();
       }
       gs.log("email_text "+ email_text + "imagename"+ imagename + "Email_log" + email_log.sys_id);

})(current, previous);


################CODE END#########################


Case:2


I did a script to prevent just logos from emails getting attached to records.
I have created a business rule on sys_attachment table

When -> Before
--Insert

File Name contains 'image'

Advanced
Script that does the check and deletion of the attachment record

Logic Used :
syslog table captures details of the EMAIL record that has created the attachment and the name of the attachment.
sys_email table captures details of the in bound email and the target(instance) record reference.
I am trying to find out the latest email record that has created the attachment, in order to get that I run a glide record on sys_email where target(instance) = table sys id field from sys_attachment table
Once I get the sys id of the sys_email record, I build the source text which follows below convention.

EMAIL.sys_id
Message contains imagename.

if the syslog matches the email record for the attachment record table sys id, then attachment will be deleted.

################CODE START#########################

(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);

################CODE END#########################

To update work-notes with the Email body !!!!!!!!!!

As per my understanding, you want to update work-notes with the mail body received by service now.
There must be a inbound email for your table. E.g Update Incident (BP) for incident table.
The inbound script will have a line something like

current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;

You can comment that line and write a new line


current.work_notes = "reply from: " + email.origemail + "\n\n" + email.body_text;

This will update work-notes instead of additional comments.

Note: Make sure which inbound script is running for your table. Also if the inbound script is updating work-notes
somewhere else in script, you have to comment it, or append all the work-notes together.

 

Thanks,

Nikhil Dixit

 

 

Please mark this post correct and helpful.

naam
Giga Contributor

Nikhil, 

Thank you for these answers here but what i am really dealing is that when an end user is replying back to the email, the whole email(including pictures, icon, email signature, disclosure notice, etc) is getting captured in the comments. so even though the comment is two line the notes attached could be 2 pages big. What i am looking for is to capture the main body of the email only and not the email attachments. I am also looking to not capture the email signature, icons, pictures, etc.