How to include previous Sent and Received mails of an incident in SEND EMAIL UI Action?

subhadeep1618
Tera Guru

I have this requirement:

User opens an incident record.

There are previous 4 emails in the activity list.

User then clicks on SEND EMAIL UI action button.

In the email box, can we include the previous mails as a mail chain and then show the prompt to write the new mail?

Similar to what happens when we click Reply in Outlook or Gmail - previous mails are shown below as history/chain.

So, is this possible to achieve in ServiceNow Send Email UI Action of incidents and change requests?


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.
3 REPLIES 3

SwatiYeginati
Tera Expert

Any update on this? I have a similar requirement.

Community Alums
Not applicable

Hi @SwatiYeginati ,

Looking at the requirement i can say you can follow the below approach-

1. Create a Script Include that fetches the previous email conversations for the incident from sys_email table and stores in email history variable.

2. Create the "Send Email" UI action to include the email history when the email form is opened.

3. Update the client script to insert the fetched email history into the email body.

Another approach would be to get emails from journal table but i wont recommend it as its a huge table can cause performance issue.

The script include snippet-

var EmailUtils = Class.create();
EmailUtils.prototype = {
    initialize: function() {},

    getEmailHistory: function(incidentSysId) {
        var emailHistory = '';
        var gr = new GlideRecord('sys_email');
        gr.addQuery('instance', incidentSysId);
        gr.orderByDesc('sys_created_on');
        gr.query();
        while (gr.next()) {
            emailHistory += '<br><br>--- ' + gr.type.getDisplayValue() + ' by ' + gr.user_display_name.getDisplayValue() + ' at ' + gr.sys_created_on.getDisplayValue() + ' ---<br>';
            emailHistory += gr.body_text.getDisplayValue();
        }
        return emailHistory;
    },

    type: 'EmailUtils'
};

 

UI Action-

function sendEmail() {
    var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
    var dialog = new dialogClass('email_client_dialog');
    dialog.setTitle(getMessage('Send Email'));
    dialog.setPreference('table', 'incident');
    dialog.setPreference('sys_id', g_form.getUniqueValue());
    var incidentSysId = g_form.getUniqueValue();
    var emailUtils = new EmailUtils();
    var emailHistory = emailUtils.getEmailHistory(incidentSysId);
    g_scratchpad.email_body = emailHistory;
    dialog.render();
}

 

 

Client Script-

(function() {
    var emailBody = g_scratchpad.email_body || '';
    if (emailBody) {
        g_form.setValue('body', emailBody + '<br><br>' + g_form.getValue('body'));
    }
})();

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

 

Thanks @Community Alums for detailing the solution.

I will try this out on my PDI and definitely let you know shortly.


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.