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

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

@neil_b 

Thank you for marking my response as helpful.

💡 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

Hi @Ankur Bawiskar I don't see a field on the sys_email table for body_html.

body html.png

 

Also, when emailing to ServiceNow, I'm noticing the content type is multipart. Will that conflict with anything? I'm not sure why it's showing as multipart. See below:

Content Type.png

 

Also, I tried the code you suggested but not my inbound action isn't getting triggered. I've added gs.info logs in the script and none of it is being logged. I'm going try commenting out certain lines of code to see why it's no longer running.

@neil_b 

try debugging line by line

 

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