Issue with Case Description Displaying Codes in Email Updates?

Mohamed Elsayed
Tera Expert

Hi All,

 

In ServiceNow, when the system sends a case update to customers, it includes only the last three additional comments. However, we want it to show all additional comments in the email update, not just the last three. We configured the email script below, which works great.

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    // Add your code here
    (function() {
        // Initialize variables
        var updates = '';
        var gr = new GlideRecord('sys_journal_field');
        gr.addQuery('element_id', current.sys_id); // Filter by the current record's sys_id
        gr.addQuery('element', 'comments'); // Filter to include only comments
        gr.orderByDesc('sys_created_on'); // Order by creation date
        gr.query();
        // Function to check if a user is internal
        function isInternalUser(userEmail) {
            if (userEmail.toLowerCase() === 'system') {
                return true; // Consider 'System' user as internal
            }
            var user = new GlideRecord('sys_user');
            user.addQuery('email', userEmail);
            user.query();
            if (user.next()) {
                var userId = user.sys_id.toString();
                var userRole = new GlideRecord('sys_user_has_role');
                userRole.addQuery('user', userId);
                userRole.addQuery('role.name', 'IN', ['snc_internal', 'itil']); // Check for snc_internal and itil roles
                userRole.query();
                var roles = [];
                while (userRole.next()) {
                    roles.push(userRole.role.name.toString());
                }
                return roles.length > 0;
            } else {
                return false;
            }
        }
        // Function to get user name by email
        function getUserNameByEmail(userEmail) {
            var user = new GlideRecord('sys_user');
            user.addQuery('email', userEmail);
            user.query();
            if (user.next()) {
                return user.name.toString();
            } else {
                return userEmail; // Return email if user not found
            }
        }
        // Loop through the updates and concatenate them
        while (gr.next()) {
            var userEmail = gr.sys_created_by.toString();
            var userName = getUserNameByEmail(userEmail);
            var isInternal = isInternalUser(userEmail);
            var backgroundColor;
            if (userEmail.toLowerCase() === 'system') {
                backgroundColor = '#d3d3d3'; // Darker gray for 'system' user
            } else {
                backgroundColor = isInternal ? '#FFFFFF' : '#f0f0f0'; // Internal comments in White, external comments in light gray
            }
            updates += '<tr style="background-color:' + backgroundColor + ';">';
            updates += '<td style="padding: 10px; border: none; border-radius: 5px; font-family: Helvetica; font-size: 10pt;">';
            updates += '<b>' + gr.sys_created_on.getDisplayValue() + ' - ' + userName + ':</b><br>';
            updates += '<div style="padding: 10px; margin-top: 5px; border-top: 1px solid #ccc; font-family: Helvetica; font-size: 10pt;">' + gr.value.replace(/\n/g, '<br>') + '</div>';
            updates += '</td>';
            updates += '</tr>';
        }
        // Add the updates to the email body
        template.print('<html><body style="font-family: Helvetica, sans-serif; line-height: 1.6; font-size: 10pt;">');
        template.print('<table style="width: 95%; max-width: 850px; border-collapse: collapse; margin: auto; border: none;">');
        template.print(updates);
        template.print('</table>');
        template.print('<table style="width: 95%; max-width: 850px; border-collapse: collapse; margin: auto; border: none; margin-top: 10px;">');
        template.print('<tr style="background-color: #d3d3d3;">'); // Darker gray for case description
        template.print('<td style="padding: 10px; border: none; border-radius: 5px; font-family: Helvetica; font-size: 10pt;">');
        template.print('<b>Case Description:</b><br>');
        template.print('<div style="padding: 10px; margin-top: 5px; border-top: 1px solid #ccc; font-family: Helvetica; font-size: 10pt;">' + current.description.replace(/\n/g, '<br>') + '</div>');
        template.print('</td>');
        template.print('</tr>');
        template.print('</table>');
        template.print('</body></html>');
    })();
})(current, template, email, email_action, event);

 

However, some customers, typically in Asia, have reported an issue where the case description includes some codes, as shown in the screenshot below:

MohamedElsayed_0-1745829551107.png

 

Any thoughts?

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Mohamed Elsayed 

seems somebody has entered HTML content in the work notes and hence it's showing that style etc

Did you verify it in sys_journal_field table?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thanks @Ankur Bawiskar,

I do not think it is related to "work notes" as we created a GlidRecord for "sys_journal_field" then filtered it to show only element: comments.

 

@Mohamed Elsayed 

did you print if any of that comment has HTML content in it?

print just before this line of code where you are getting the value

AnkurBawiskar_0-1745835965939.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Mohamed Elsayed
Tera Expert

When I check the email in ServiceNow, it goes out without these HTML codes. This might indicate that the issue is related to how the email client interprets the HTML content.