How to copy sent/received emails from incident record to a record in another table

jlaue
Kilo Sage

Hello - 

I am working on a UI Action on the Incident table that will create a new record in a custom table and copy specific fields, attachment(s), work notes/comments from an incident. 

I have all of this working, however there is also a requirement to copy the sent/received emails that are in the activity log of the incident over to the new record in the custom table.  I have searched around Community, but couldn't find a way to accomplish this.  I believe they are referenced from the sys_email table when they are displayed on Sent/Received activity on the activity log, but not sure how to replicate that on the new record I am creating.

Thanks!

 

1 ACCEPTED SOLUTION

Stephen Sturde
Tera Guru

Hi jlaue,

You're correct. The emails in the activity log are actually reference entries to the sys_email table. If you truly need to duplicate them, I would look up the records related to the incident in the email table, and insert new versions with updates to the 'target' and 'target_table' values on the email. The newly inserted emails with the correct targets will show up in the activity log of the newly created custom record.

Let me know if you have any trouble with this.  

-Stephen

View solution in original post

4 REPLIES 4

Stephen Sturde
Tera Guru

Hi jlaue,

You're correct. The emails in the activity log are actually reference entries to the sys_email table. If you truly need to duplicate them, I would look up the records related to the incident in the email table, and insert new versions with updates to the 'target' and 'target_table' values on the email. The newly inserted emails with the correct targets will show up in the activity log of the newly created custom record.

Let me know if you have any trouble with this.  

-Stephen

jlaue
Kilo Sage

That did the trick!   Here is the code I used in my UI Action in case it can help anyone else with this requirement:

 

var incemail = new GlideRecord('sys_email');

incemail.addQuery('instance', current.sys_id);
incemail.orderByDesc('sys_created_on');
incemail.query();

while (incemail.next()) {

var taxemail = new GlideRecord('sys_email');

taxemail.initialize();
taxemail.instance = sysID;
taxemail.target_table = "TARGET TABLE NAME HERE";
taxemail.type = incemail.type;
taxemail.sys_created_on = incemail.sys_created_on;
taxemail.user = incemail.user;
taxemail.subject = incemail.subject;
taxemail.recipients = incemail.recipients;
taxemail.body = incemail.body;
taxemail.content_type = incemail.content_type;
taxemail.headers = incemail.headers;
taxemail.sys_created_by = incemail.sys_created_by;
taxemail.sys_updated_on = incemail.sys_updated_on;
taxemail.sys_updated_by = incemail.sys_updated_by;
taxemail.autoSysFields(false);    //this allows us to use the sys_created_by date of the copied records so that //the emails retain the correct order in the activity view of the new record
taxemail.update();

}

Chia Wei
Tera Contributor

I have a scenario where I need to move the emails from one record to another in the same table.Tried updating the sys_email.instance to the new sys_id of the record but it did not get show up in the activities log of the new sys_id. From the sys_email table, i tried filter on the instance and get 5 records but click into the case it didn't show all 5 in the activities log.

 

Any thoughts on what I might need to update other than the target table and instance?

PJNATH
Tera Contributor

thanks