Forward/reply all email button to capture and retain all previous senders in the body activity

Sam198
Mega Guru

Hi

I have a requirement to capture and stamp all the previous "To" and "cc" email addresses in the email body when someone is using Reply all and Forward button.

Currently, it only stamps the from email address as below, we need to include all the email addresses that were on the original email that a user reply all or forward from, is this possible?

 

find_real_file.png

2 REPLIES 2

shloke04
Kilo Patron

Hi,

Please check if below thread helps you:

https://community.servicenow.com/community?id=community_blog&sys_id=df609af8dbbbdb405ed4a851ca961928

https://community.servicenow.com/community?id=community_question&sys_id=614c6a48dbc1a30c5129a851ca961957

So now once you have configured the steps mentioned in above thread, in order to fetch all previous Recipient in To and CC and add them , please follow the steps below:

1) Create a Notification Email Script and use the script below:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    var recordID = current.sys_id;
    var rec = getRecipients(recordID);
    var rec1 = getCCRecipients(recordID);
    email.addAddress("cc", rec1);
    email.setReplyTo(rec);



    function getRecipients(recordID) {
        var arr = [];
        var gr = new GlideRecord('sys_email');
        gr.addQuery('instance', recordID);
        gr.query();
        while (gr.next()) {
            arr.push(gr.recipients.toString());

        }
        return arr1.toString();
    }

    function getCCRecipients() {
        var arr1 = [];
        var gr = new GlideRecord('sys_email');
        gr.addQuery('instance', recordID);
        gr.query();
        while (gr.next()) {
            arr1.push(gr.blind_copied.toString());
        }
        return arr1.toString();
    }

})(current, template, email, email_action, event);

Not sure the above script is not a tested one, you may need to check to ensure this does not affect performance and as we are querying the Email Log table which is not good I believe as it has lot of data and may impact.

Please do through testing and let me know if you are stuck.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hi Sholke,

Thanks for the reply, but I am not after adding To and CC users in those fields, I am after capturing those all TO and CC users in the email client box when someone reply all or forward to the original email same as you would see in Oulook (format does not matter) 

This email client box is stamped with the below email script - insert_original_email_message, where we can see its only capturing the "uname" "template.print("On " + dateTimeFormat+", '" + uname + "' wrote:\n\n");"

- the requirement is to capture all TO and CC users instead of just the "From user" - "uname = getFormatedNameAndEmailFromUser(user);"

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
// This mail script is intended to be used by the response client templates and will only work as expected
// if called from a template with a content type of HTML. current represents the original sys_email record.
var user = new GlideRecord('sys_user');
var uname = current.user.toString();

if (current.user_id && user.get(current.user_id))
uname = getFormatedNameAndEmailFromUser(user);
else if (current.type != 'received') {
//Try and get the user that created the email
var createByName = current.sys_created_by;
if (user.get('user_name', createByName))
uname = getFormatedNameAndEmailFromUser(user);
else if (!current.user) {
//otherwise use the info from the SMTP account
var emailAccount = new GlideRecord("sys_email_account");
emailAccount.addActiveQuery();
emailAccount.addQuery("type", "smtp");
emailAccount.query();
if (emailAccount.next()) {
var userLabel = "";
var fromAddress = "";
if (emailAccount.email_user_label)
userLabel = emailAccount.email_user_label.toString() + " ";
if (emailAccount.from)
fromAddress = '<' + emailAccount.from.toString() + '>';

uname = userLabel + fromAddress;
}
}
}

//If we somehow never got an identifier for the sender go with one of these two defaults
if (!uname)
if (current.user)
uname = current.user.toString();
else
uname = "System";


var usePlainTextResponseContent = gs.getProperty('glide.email_client.use_plain_text_response', 'false');

var pattern = "EEEE dd, MMMMM hh:mm:ss a z";
var simpleDateFormat = new Packages.java.text.SimpleDateFormat(pattern);
simpleDateFormat.setTimeZone(Packages.java.util.TimeZone.getTimeZone(gs.getSession().getTimeZoneName()));
var dateTimeFormat = simpleDateFormat.format(new Packages.java.util.Date(new GlideDateTime(current.sys_created_on).getNumericValue()));


if ((usePlainTextResponseContent == 'true' && !current.body_text.isNil()) || current.body.isNil()) {
template.print("On " + dateTimeFormat+", '" + uname + "' wrote:\n\n");
// Process plain text to dislay in HTML email client
template.print(sn_notification.EmailResponse.getOriginalEmailTextContent(current.getUniqueValue()));
} else {
template.print("<div>On " + dateTimeFormat +", '" + uname + "' wrote:<br />");
template.print("<blockquote>");
// Remove html, head, body and style tags
template.print(sn_notification.EmailResponse.getOriginalEmailHtml(current.getUniqueValue()));
template.print("</blockquote></div>");
}

function getFormatedNameAndEmailFromUser(/*GlideRecord*/ user) {
var userName = user.name.toString();
if (user.email)
userName += ' &lt;' + user.email.toString() + '&gt;';

return userName;
}
})(current, template, email, email_action, event);