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

I found the solution: created 'before insert' BR on the table 'sys_email' to create watermark and update target.


Then the incoming email email is processed by 'Reply' inbound email action, the ticket is updated using variable 'current' and the email is displayed in the activity log.



function onBefore(current, previous) {


  var ticketID = (new MCUtil()).getMCTicketID(current.subject);



  // create watermark


  var wm = new GlideRecord("sys_watermark");


  wm.initialize();


  wm.email = current.sys_id;


  wm.source_table = 'sc_task';


  wm.source_id = ticketID;


  wm.insert();


  var wmText = 'Ref:' + wm.number;



  // add watermark to the email


  current.body = current.body + '\n' + wmText;


  current.body_text = current.body_text + '\n' + wmText;



  // update target and type


  current.target_table = 'sc_task';


  current.instance = ticketID;


  current.receive_type = 'reply';


}


Watermarks are intended for outgoing emails only - it does not make sense to add it to incoming emails.   And why change the "receive_type" field?



What conditions do you have on the Business Rule?   You'll want to make sure it only runs for a limited set of emails otherwise it will run for them all.   I still think you are better off updating the email record from the Inbound Email Action - it would be self-contained in one script.


it does not make sense to add it to incoming emails

Are you sure? What about the WIKI: Inbound Email Actions - ServiceNow Wiki?



What conditions do you have on the Business Rule?   You'll want to make sure it only runs for a limited set of emails otherwise it will run for them all.


Of course I added the contition to process appropriate emails only.



I still think you are better off updating the email record from the Inbound Email Action - it would be self-contained in one script.


Have you tried to implement your approach? I didn't manage to make it work. In this case the system recognized the email as 'New' and created new ticket instead of updating target ticket. It would be great if you show me working script.


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.


Hi Jym,



Thanks a lot for your help!



Your approach much better than my. It's enough to do this to link email:



  1.   //now update the email record so it will show up in the record's activity formatter  
  2.   sys_email.target_table = "incident";  
  3.   sys_email.instance = gr.getValue("sys_id");  
  4.   sys_email.update();