Email script is not working in the subject
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2023 07:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 01:35 AM - edited 08-29-2023 01:46 AM
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*/ ');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 02:05 AM
what's your business requirement here?
Did you add logs and debug?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 03:32 AM
@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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 09:06 AM
Try this - this is just an example of what Sandeep suggested. So you can use template.print(), in a notification email script.