Show only most recently updated field in email notification

Marcel H_
Tera Guru

In the interest of reducing the number of email templates that I have to use, I’m trying to create one that would display the Additional Comments OR Work Notes OR Close Notes on a notification for groups that what to be notified whenever one of these fields changes. It’s easy enough to add all to the email template, but what I’m not sure of is how I can display only the most recently updated field and not the others.

Currently if I update comments save the record I get the comments in the notification, but if I then add work notes and save, I get work notes and the last comment made. I’d like to have all the fields on the template, but only display whichever was the most recently updated and omit the others.

I imagine this could be done with a mail script, but not sure where to start.

4 REPLIES 4

Saurav Maiti
Mega Expert

What code are you at present in  email script?

Marcel H_
Tera Guru

Currently I call one of two possible mail scripts in my notifications, one for new comments and one for new work notes. These mail scripts basically just get the latest entry and strip out the preceding information (commenter name, time stamp, etc.) so that all you get is the text of the comment. I have another mail script that pulls the name and initials of the commenter as shown below:

find_real_file.png

 

The code in the mail scripts for comments and work notes are below:

Comments:

var jString = current.comments.getJournalEntry(1);  
var nString = setJournalString(jString);
template.print(nString);  
  
function setJournalString(jString) { 
    var regex = /\d+\W\d+\W\d+\s(.+)\n/;   
    var result = jString.replace(regex, '');  
    return result;  
}  

Work Notes:

var jString = current.work_notes.getJournalEntry(1);  
var nString = setJournalString(jString);
template.print(nString);  
  
function setJournalString(jString) { 
    var regex = /\d+\W\d+\W\d+\s(.+)\n/;   
    var result = jString.replace(regex, '');  
    return result;  
} 

 

 

 

 

 

Marcel H_
Tera Guru

I'm wondering if something similar to the @Mention emails and the related script could be used to get the field that was updated last? I'm not sure if the if (current.field_name == "comments") would work on outside of the live_notification table that those notifications come from.

 

var recordGR = new GlideRecord(current.table);

if(recordGR.get(current.document)) {

         email.setSubject("You have been mentioned in " + recordGR.number +" " + recordGR.short_description);

       template.print("<p style='color: #646464; margin: 10px 0; padding: 0; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; color: #646464; font-family: Gill Sans, Helvetica Neue, Helvetica, Arial,' sans-serif'; font-size: 16px; line-height: 150%; text-align: left;'>You have been mentioned by " + current.user_from.name + " in <a href= '" + recordGR.getRecordClassName() + ".do?sys_id=" + recordGR.getUniqueValue() + "' style='color:#0564A9; font-weight:bold'>" + recordGR.getDisplayValue() + "</a></p>");

       if (current.field_name == "comments") {

               template.print(recordGR.comments.getJournalEntry(1) +   "<br />");

       }

       else if (current.field_name == "work_notes") {

               template.print(recordGR.work_notes.getJournalEntry(1) +   "<br />");

       }

}

saiiaskumar
Mega Guru

I Think you have to query sys_journal_field table where every activity is recorded there.

 

var gr = new GlideRecord('sys_journal_field');
gr.addEncodedQuery("element=work_notes^ORelement=comments^name="+current.getTabeName()+"^element_id="+current.getUniqueValue());// You will get only comments and work notes of this record.

gr.orderBy("sys_created_on");//Get in order by created date
gr.query();

if(gr.next()){//It will get only one record and latest one

if (gr.field_name == "comments") {

template.print(gr.comments.getJournalEntry(1) + "<br />");

}

else if (gr.field_name == "work_notes") {

template.print(gr.work_notes.getJournalEntry(1) + "<br />");

}

}