Inbound Action Not Formatting Email Correctly and Not Sending To The Correct Group

neil_b
Tera Guru

Hi,

 

Goal: To send an email to ServiceNow and have an Inbound Action forward the email to a specific group of users.

 

I have an inbound action created and here is the script:

    var groupSysID = '12345678910'; 	
	var subject = email.subject;
	var body = email.body_text;
	var mailType = "send-ready";

        // Create a new email record in the Outbox
        var mail = new GlideRecord("sys_email");
        mail.initialize();
        mail.mailbox.setDisplayValue("Outbox"); // Place the new email in the Outbox
        mail.recipients = groupSysID; 
        mail.subject = subject;
        mail.body = body;
        mail.type = mailType;

if (email.body_html) {
            mail.body_html = email.body_html;
            mail.content_type = "text/html";
        } else {
            mail.content_type = "multipart/mixed"; // Default to multipart/mixed if no HTML
        }

	mail.insert();

 

Issue 1: The notification isn't sending to the group. I assume I'm not able to use a group sys_id for the recipients field? I noticed that if I put a specific user in that field, the notification does send.

 

Issue 2: When the notification does send, it's not formatting the email correctly. Please see below. 

This is the inbound email I'm sending to ServiceNow:

Email inbound preview.png

When the email is forwarded to the user, it is presented like this:

Email forwarded preview.png

 

It's somehow removing all of the HTML from the body and body_text and just showing plain text.

 

Any ideas?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@neil_b 

you need to include all group members

set content type as text/html

var groupSysID = '12345678910'; // Group sys_id
var subject = email.subject.toString();
var bodyText = email.body_text.toString();
var bodyHtml = email.body_html ? email.body_html.toString() : '';

var mail = new GlideRecord("sys_email");
mail.initialize();
mail.type = "send-ready";
mail.subject = subject;

// CRITICAL: recipients accepts COMMA-SEPARATED sys_ids (users/groups)
var groupMembers = getGroupMembers(groupSysID);
mail.recipients = groupMembers.join(',');

// Preserve original formatting
if (bodyHtml) {
    mail.body_html = bodyHtml;
    mail.body = bodyHtml; // Fallback for plain text clients
    mail.content_type = "text/html";
} else {
    mail.body = bodyText;
    mail.content_type = "text/plain";
}

mail.insert();

function getGroupMembers(groupId) {
    var members = [];
    var grMem = new GlideRecord('sys_user_grmember');
    grMem.addQuery('group', groupSysID);
    grMem.addActiveQuery();
    grMem.query();
    while (grMem.next()) {
        members.push(grMem.user.toString());
    }
    return members;
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Hi @Ankur Bawiskar I got it working! I just had to dot walk to the user's email address instead of sys_id and that worked! Here is the final code below:

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

	// Prep fields
    var groupSysID = 'sys_id'; 
    var subject = email.subject;
    var bodyHtml = email.body_html;
    var bodyText = email.body_text;
    var mailType = "send-ready";

    // Create a new email record in the Outbox
    var mail = new GlideRecord("sys_email");
    mail.initialize();
    mail.mailbox.setDisplayValue("Outbox"); // Place the new email in the Outbox
	mail.subject = subject;
    mail.type = mailType;

	// Get list of group members
    var groupMembers = getGroupMembers(groupSysID);
	mail.recipients = groupMembers.join(',');

    // Include HTML body if available and set content type
    if (bodyHtml) {
        mail.body_html = bodyHtml;
        mail.body = bodyHtml; // Fallback for plain text clients
        mail.content_type = "text/html";
    } else {
        mail.body = bodyText;
        mail.content_type = "text/plain";
    }

	// Insert email record
    mail.insert();

	// Function to retrieve list of users from group
    function getGroupMembers(groupId) {
        var members = [];
        var grMem = new GlideRecord('sys_user_grmember');
        grMem.addQuery('group', groupSysID);
        grMem.addActiveQuery();
        grMem.query();
        while (grMem.next()) {
            members.push(grMem.user.email.toString());
        }
        return members;
    }

})(current, event, email, logger, classifier);

 

Thank you so much as always!!!