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.

Email script is not working in the subject

Brijmohan
Tera Contributor

I am calling email script in Notification subject. It is not working in the subject and same script working in the body. PFB script that I am using in email script.

 

var str = current.short_description.toString();
                var str1 = str.slice(0,10);
                template.print(str1);
                //email.setSubject('Information request '+current.number+' '+str1);

email.setSubject is working in subject but in that case I need to create separate email script for each notification.

 

Thanks & regards,

Brijmohan  

8 REPLIES 8

Madhav Rao D V
Tera Expert

Hi @Brijmohan ,
According to your requirements, The expected outcome is to set the subject of the email notification based on what notification.
Using the field called email_action.name(Get the name of the notification) and a set of if-else conditions, we can set the subject of the email according to the requirement.
if(email_action.name == '/*Name of the notification*/'){
email.setSubject(' /*Content*/ ');

}

else if(email_action.name == '/*Name of the notification*/'){
email.setSubject(' /*Content*/ ');

}

Ankur Bawiskar
Tera Patron
Tera Patron

@Brijmohan 

what's your business requirement here?

Did you add logs and debug?

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

Sandeep Rajput
Tera Patron
Tera Patron

@Brijmohan To address your requirement related to using a single subject script for different notifications, you can simply create a generic subject mail script, inside the subject script, simply use a switch case and add multiple cases with notification name. 

 

Here is an example.

 

@Brijmohan To address your requirement related to using a single subject script for different notifications, you can simply create a generic subject mail script, inside the subject script, simply use a switch case and add multiple cases with notification name. 



Here is an example.

(function runMailScript(current, template, email, email_action, event) {

        var priority = [current.priority.getDisplayValue()];

        var subject = "";
        switch (email_action.name.toString()) {
            case “ Comment added”:
                subject = "Case opened - " + current.number + ": " + variables;
                break;
            case “ Work note added":
                subject = "Case updated - " + current.number + ": " + variables;
                break;
        }
    }
 

thomaskennedy
Tera Guru

Try this - this is just an example of what Sandeep suggested. So you can use template.print(), in a notification email script.

 

thomaskennedy_0-1693325080218.png

 

thomaskennedy_1-1693325144919.png