- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2018 07:37 AM
Hi
I am trying to add the most recent Sent/Received email journal field to a client email template but I cant seem to get it to work.
I found this script to add work notes to the email to see if it works so I can then amend it to show the most recent Sent/Received email but it doesn't seem to return anything, What am I doing wrong?
var notes = current.work_notes.getJournalEntry(-1); //gets all journal entries as a string where each entry is delimited by '\n\n' var na = notes.split("\n\n"); //stores each entry into an array of strings for (var i = 0; i < na.length; i++) gs.print(na[i]);
Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2018 07:58 AM
Hi Russel,
The code you have mentioned above is correct. I believe, the implementation you are trying to achieve is confusing/semantically incorrect.
If I understand your question right, you want to show the Sent/Received emails(shown on the activity of a record) on an outbound email?
This is possible only if it is an actual field. The data showed in the activity log is based on Audit table and relations to sys_email table. So may want to create a code for the same like:
var gr = new GlideRecord("sys_email");
gr.addQuery("instance",current.sys_id);
gr.orderBy("sys_created_on");
gr.query();
//Now gr will contain all the emails sent or received for the target record.
//If you want to restrict is to a number then
var gr = new GlideRecord("sys_email");
gr.addQuery("instance",current.sys_id);
gr.setLimit(3);//put the max number here
gr.orderBy("sys_created_on");
gr.query();
//The field on the addQuery statement was a mistake.It should be "instance". This would do the trick.
Make sure you let us know, how it went after trying this.
Regards,
Sagar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2018 07:58 AM
Hi Russel,
The code you have mentioned above is correct. I believe, the implementation you are trying to achieve is confusing/semantically incorrect.
If I understand your question right, you want to show the Sent/Received emails(shown on the activity of a record) on an outbound email?
This is possible only if it is an actual field. The data showed in the activity log is based on Audit table and relations to sys_email table. So may want to create a code for the same like:
var gr = new GlideRecord("sys_email");
gr.addQuery("instance",current.sys_id);
gr.orderBy("sys_created_on");
gr.query();
//Now gr will contain all the emails sent or received for the target record.
//If you want to restrict is to a number then
var gr = new GlideRecord("sys_email");
gr.addQuery("instance",current.sys_id);
gr.setLimit(3);//put the max number here
gr.orderBy("sys_created_on");
gr.query();
//The field on the addQuery statement was a mistake.It should be "instance". This would do the trick.
Make sure you let us know, how it went after trying this.
Regards,
Sagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2018 02:21 AM
This is great thanks, however, do I need to do a template.print or something? I have applied the code to limit the entries but it doesn't add anything to the email.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2018 03:22 AM
Ok, so I have worked out to get the last sent email to be printed to the email:
var gr = new GlideRecord("sys_email");
gr.addQuery("target",current.sys_id);
gr.setLimit(1);//put the max number here
gr.orderByDesc("sys_created_on");
gr.query();
gr.next();
template.print(gr.getValue('body'));
I hope this is correct? im still teaching myself to code.
However, there is one problem, it seems to print the last sent email no matter what record you are in. When I try to send an email from another incident record it prints the last email sent, but the last email sent was in a different record.
How are the emails linked to the record so that it can be displayed in the activity?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2018 04:09 AM
Hi Russel,
I have modified the code a little bit. You are right in using "template.print".
The change in the code was updating the "target" to "instance". This would get you the right email from the target record.
Russel, Please mark the answer as correct and helpful if it was 🙂
Thanks,
Sagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2018 04:17 AM
One set ahead of you, realised it should have been instance instead of target and its working now! 🙂
Thanks so much for your help