Contents of Email Notification are in Wrong Order When Viewed in Email Logs

cwolfe01
Giga Guru

Hi all,

 

I've been doing some work with notifications, and have made some progress, but I've stumbled across a problem I just can't figure out.

 

Here's the code I'm using:

(function runMailScript(current, template, email, email_action, event) {
	
	template.print('<p>Greetings, <br><br>The incident below has been opened on your behalf. <br><br></p>');
	
	var incident = new GlideRecord("incident");
	incident.addQuery('sys_id', current.sys_id);
	incident.query();

	while (incident.next()) {
		
		template.print('<p>Incident: ' + incident.number + '<br>');
		template.print('Short Description: ' + incident.short_description + '<br><br>');
		template.print('</p>');

		template.print('<p>Additional Details: <br></p>');

		template.print('<table style=\'border:1px solid black\'><tbody>');
			template.print('<tr>');
				template.print('<td style=\'border:1px solid black\'>Caller</td>');
				template.print('<td style=\'border:1px solid black\'>${caller_id}</td>');
			template.print('</tr>');
			template.print('<tr>');
				template.print('<td style=\'border:1px solid black\'>Priority</td>');
				template.print('<td style=\'border:1px solid black\'>${priority}</td>');
			template.print('</tr>');
			template.print('<tr>');
				template.print('<td style=\'border:1px solid black\'>Service</td>');
				template.print('<td style=\'border:1px solid black\'>${business_service}</td>');
			template.print('</tr>');
			template.print('<tr>');
				template.print('<td style=\'border:1px solid black\'>Service Offering</td>');
				template.print('<td style=\'border:1px solid black\'>${service_offering}</td>');
			template.print('</tr>');
			template.print('<tr>');
				template.print('<td style=\'border:1px solid black\'>State</td>');
				template.print('<td style=\'border:1px solid black\'>${state}</td>');
			template.print('</tr>');
		template.print('</table></tbody>');
		
		template.print('<br><br>');

		var instanceName = gs.getProperty('instance_name');
		var link = 'https://' + instanceName + '.servicenowservices.com/sp?id=ticket&table=incident&sys_id=' + current.sys_id;

		template.print('You can view all the details of the incident by following the link below: <br><br><br>');

		var backgroundColor = 'background-color: #278efc;';
		var border = 'border: 1px solid #0368d4;';
		var color = 'color: #ffffff;';
		var fontSize = 'font-size: 16px;';
		var fontFamily = 'font-family: Helvetica, Arial, sans-serif;';
		var textDecoration = 'text-decoration: none; border-radius: 3px;';
		var webKitBorder = '-webkit-border-radius: 3px;';
		var mozBorder = '-moz-border-radius: 3px;';
		var display = 'display: inline-block;';
		var padding = 'padding: 5px;';

		if (email_action.name == "Incident Survey") {
			color = 'color: #343d47;';
			backgroundColor = 'background-color: #e6e8ea;';
			border = 'border: 1px solid #bdc0c4;';
		}

		template.print('<a href="' + link + '"');
		template.print('style="' + backgroundColor + border + color + fontSize + fontFamily + textDecoration + webKitBorder + mozBorder + display + padding);
		template.print('">');
		template.print(gs.getMessage('Take me to the Incident'));
		template.print('</a>');
		template.print('<br><br>');
		template.print('<p><font size="3">');
		template.print('Thank you.');
		template.print('</p>');
	}

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

Everything looks fine when I preview the notification as seen below:

Screenshot 2023-08-18 120631.png

But when I view the email through the email logs, there are some parts out of order:

Screenshot 2023-08-18 120605.png

 

I've identified that the issue starts on line 42 of my code with var instanceName, but I'm not entirely sure what's causing the issue. Can anyone help?

2 REPLIES 2

Tushar
Kilo Sage
Kilo Sage

Hi @cwolfe01 

 

To resolve this, you should properly structure your template and concatenate the variables and HTML elements correctly.

 

Here's a revised version of your code to address this issue:

 

(function runMailScript(current, template, email, email_action, event) {
    template.print('<p>Greetings, <br><br>The incident below has been opened on your behalf. <br><br></p>');

    var incident = new GlideRecord("incident");
    incident.addQuery('sys_id', current.sys_id);
    incident.query();

    while (incident.next()) {
        // Other parts of your template before the link

        var instanceName = gs.getProperty('instance_name');
        var link = 'https://' + instanceName + '.servicenowservices.com/sp?id=ticket&table=incident&sys_id=' + current.sys_id;

        var linkStyles = 'background-color: #278efc; border: 1px solid #0368d4; color: #ffffff; font-size: 16px; font-family: Helvetica, Arial, sans-serif;';
        var linkDecoration = 'text-decoration: none; border-radius: 3px; -webkit-border-radius: 3px; -moz-border-radius: 3px; display: inline-block; padding: 5px;';

        if (email_action.name == "Incident Survey") {
            linkStyles = 'background-color: #e6e8ea; border: 1px solid #bdc0c4; color: #343d47; font-size: 16px; font-family: Helvetica, Arial, sans-serif;';
            linkDecoration = 'text-decoration: none; border-radius: 3px; -webkit-border-radius: 3px; -moz-border-radius: 3px; display: inline-block; padding: 5px;';
        }

        var linkHtml = '<a href="' + link + '" style="' + linkStyles + linkDecoration + '">' + gs.getMessage('Take me to the Incident') + '</a>';
        template.print(linkHtml);

        // Other parts of your template after the link
    }
})(current, template, email, email_action, event);

 


Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

  1. Regards,
    Tushar

Hi Tushar,

 

After implementing these changes my problem is still persisting. Many thanks for your help!