Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Appending to an email subject line in an email notification

Paul Ciarfella
Tera Guru

Let's say that I have an email notification with a subject line something like:
${number} ${company} has been assigned to group ${assignment_group}

Is there a way to append to the subject line in a mail script, such that I could tack on additional information depending on some dynamic qualifier within the notification? For example, I've tried something like the following in a mail script:

email.setSubject( email_action.subject + " and you better address it now!" );


But the result is an email with the appended text but the variables have not been processed:
${number} ${company} has been assigned to group ${assignment_group} and you better address it now!

I would like to have a common script that I can reference in all my email notifications, where I can use email_action.subject as the prefix, and then tack on the text at the end as needed. I'd prefer to use the email notification Subject field as a base and not embed the subject within the script.

Cheers.

Paul C

2 REPLIES 2

CapaJC
ServiceNow Employee
ServiceNow Employee

This community contributer seems to have a solution:
Setting email subject with a mail script


Paul Ciarfella
Tera Guru

I looked at the recommended solution and decided to follow that tack.

So, I cleared the Subject field in each of the affected notifications and redefined the subject in a mail script inside the body of the notification. i didn't want to do it this way but it seemed like the only (and quickest) solution. I also created a helper function / script include that would be called from the affected email notifications.

So, in the email notification I have:
gs.include("setEmailSubject");
var subject = "WARNING: " + current.number + " blah blah blah";
setEmailSubject( email, current, subject);

Where the function is defined like this (simplified):
function setEmailSubject( email, ticket, subject ) {
/*
** Determine if additional information is needed on the subject line
*/
var endText = '';
if then else logic to determine what to append to the subject line using fields from the ticket.

/*
** Append the text to the subject line
*/
email.setSubject( subject + endText );
}