How to add email to the activity log

alexbin
Kilo Contributor

Hi all,

My inbound email action works with incoming email (email type = new) as follow:

- parse external ticket number from the subject string

- find (ticket.external_number = parsed number) and update SNow ticket using new GlideRecord object (because variable 'current' is not associated with the SNow ticket)

All works as expected.

The problem is: the incoming email is not displayed in the activity log, while outgoing emails for the ticket displayed correctly.

How to fix this?

As I understand, activity log displayed information from the system audit tables. Can I add all necessary information to the tables manually?

Or maybe update somehow the email record in the mailbox before it is processed to link the email with the target ticket and use 'current' to update the ticket...

Please help!

1 ACCEPTED SOLUTION

A script similar to this should do the trick:



(function(){


  //look for the 3rd-party tool Incident


  var gr = new GlideRecord("incident");


  if (gr.get("correlation_id", email.subject)){


      //found it so let's update the SN Incident


      gr.comments = "We found it - here is the contents of the new email:\n" + email.body_text;


      //...whatever else you want to update would go here


      gr.update();


  } else {


      //did not find it so we need to create a new Incident


      gr.newRecord();


      gr.correlation_id = email.subject;


      gr.correlation_display = "Name of 3rd-Party Tool";


      gr.description = email.body_text;


      //...whatever else you want to update would go here


      gr.insert();


  }



  //now update the email record so it will show up in the record's activity formatter


  sys_email.target_table = "incident";


  sys_email.instance = gr.getValue("sys_id");


  sys_email.update();



})();



Obviously things would need to be tweaked a bit to fit your use case, such as retrieving the 3rd-party's incident number, etc...     I setup the Inbound Email Action with the following assumptions/criteria:


  • the emails that come in are always new ones, not replies to a SN email, so they would never have a watermark in them
  • the subject of the email contains only the 3rd-party tool's incident number
  • the 3rd party tool's incident number is saved in the "correlation_id" field
  • the emails are coming in from a particular user record or email address (I set mine to look for my user/email address)
  • once this script is run, we can skip the other Inbound Email Actions ("Stop processing" is checked)
  • set to run on the "Email [sys_email]" table so the "current" objet does not waste a new Incident number each time it is run
  • set to run early ("Order" = 10)
  • running on the Geneva version


find_real_file.png



This way the "integration" is neatly wrapped up in just 1 record.


View solution in original post

14 REPLIES 14

rob_pastore
ServiceNow Employee
ServiceNow Employee

Is there a reason you are not using the default watermark for incoming emails?


Unfortunately, incoming emails generated by external 3rd party system and doesn't contain SNow watermarks.


rob_pastore
ServiceNow Employee
ServiceNow Employee

I'm guessing that the default behavior that adds reply emails to the activity log comes from using the reply type of the inbound email.



Since these are new emails, but you are treating them as replies (using the INC number from the subject) it's not updating the activity log.



You could manually update the comments field with the body of the email, that would add it to the audit tables for you.


the subject does not contain SNow INC number, only external one, so the variable 'current' doesn't point to the appropriate ticket.


if I try this:


  current.get('external_number', ...);


  current.update();


the system creates new ticket instead of updating existing one.


That's why the code is like this:


  var anotherTicketObject = new GlideRecord('...');


  anotherTicketObject.get('external_number', ...);


  anotherTicketObject.update();