Notification subject change by script

Ansuman
Tera Contributor

Hi everyone,

I want to change the subject of the notification according to the instance from which it's being triggered. Like if its a request creation notification:

normal notification: Request REQ0010286 was created

I want if it is being triggered from a test instance it is to be:

TEST Instance Notification Request REQ0010286 was created

if it is being triggered from the dev instance it is to be:

DEV Instance Notification Request REQ0010286 was created

I cannot edit every notification as there are more than 80

please help

1 ACCEPTED SOLUTION

BR would be before, on insert, for the sys_email table, with code as follows:

 

var environmentPrefix= '';
var instanceName= gs.getProperty('instance_name');
switch (instanceName) {
case 'sntest' : environmentPrefix = 'TEST '; break; //replace with your test instance name
case 'yourdev' : environmentPrefix = 'DEV '; break; //replace with your devinstance name
}
var oldSubject = current.subject + '';
current.subject = (environmentPrefix + oldSubject);

 

 This is just an example (not tested) so give it a go.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

If you are using common templates or layouts amongst your notifications, you could add a mail script in them to change the subject. Code would be something like below:

var environmentPrefix= '';
var instanceName= gs.getProperty('instance_name');
switch (instanceName) {
case 'sntest' : environmentPrefix = 'TEST '; //replace with your test instance name
case 'yourdev' : environmentPrefix = 'DEV '; //replace with your devinstance name
}
email.setSubject(environmentPrefix + email_action.subject);

Mail Script API 

Failing that, you would need to create a business rule on the sys_email table to change the subject when it is created. You could use similar code to above.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

can you help me out abit more and write BR script for it. Sorry for the inconvenience 

BR would be before, on insert, for the sys_email table, with code as follows:

 

var environmentPrefix= '';
var instanceName= gs.getProperty('instance_name');
switch (instanceName) {
case 'sntest' : environmentPrefix = 'TEST '; break; //replace with your test instance name
case 'yourdev' : environmentPrefix = 'DEV '; break; //replace with your devinstance name
}
var oldSubject = current.subject + '';
current.subject = (environmentPrefix + oldSubject);

 

 This is just an example (not tested) so give it a go.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Saurav11
Kilo Patron
Kilo Patron

hello,

 

Is the notification already created and is on sc_request then do the below:-

 

Go to notification script

 

Saurav11_0-1665576727166.png

 

Write the below code

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

          // Add your code here
	var instance= gs.getProperty('instance_name');
	var name;
	if(instance.indexOf("dev")>-1)
		{
			name ='DEV';
		}
	else if(instance.indexOf("test")>-1)
		{
			name ='TEST';
		}
	else
		{
			name ='PROD';
		}
	email.setSubject(name+'Instance Request' +current.number +'was created');

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

 

Saurav11_1-1665577246733.png

 

Then in the notification call it as below:-

 

 

Saurav11_3-1665577480541.png

 

 

Please mark my answer as correct based on Impact.