Add journal entry to email client template

russellprice
Tera Contributor

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

 

1 ACCEPTED SOLUTION

Not applicable

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

View solution in original post

9 REPLIES 9

rammohanraomadd
Kilo Guru

Hi,

 

Please replace the whole script with below code for latest entry in email script and make sure there is an entry existing.

 

 

var notes = current.work_notes.getJournalEntry(1);
template.print(notes);

Regards,
Ram M.

That does bring back a result but it shows the most recent entry not the most recent email sent.

find_real_file.png

I would like it to show the email at the top but it shows the last work log instead

Hi Russell,

 

Email is not updated in work notes unless in your inbound action if you have added the script to populate the body of the email in the worknotes.

 

Regards,

Ram M

Not applicable

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("target",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("target",current.sys_id);
gr.setLimit(3);//put the max number here
gr.orderBy("sys_created_on");
gr.query();

 

Make sure you let us know, how it went after trying this.

Regards,

Sagar