The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Setting email notification dynamically based on the environment the user is in

ndejoya
Tera Contributor

Hello! 

 

Still learning the ins and outs of ServiceNow and the the different things that can be done with email notifications. I have an existing email notification that needs its conditions to be updated. Right now, in the 'Who will receive' tab, there is Groups SNOW Dev and SNOW Test. What's being asked, where I'm not sure how to go about, is the email notification should be dynamically set based on the environment you are in. Would anyone be able to guide a relatively new ServiceNow user on how to do this? Thank you for the time!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ndejoya 

how are you triggering the email? using event or directly using the notification "Inserted" and "Updated" checkboxes?

To handle dynamic recipient you should use after business rule & create event on the same table as that of the notification

Then based on your logic you can set the recipients via triggering the event.

I hope I have answered your question and you can enhance it further based on your requirement.

1) create event on your table

AnkurBawiskar_0-1744685559490.png

 

2) make notification trigger on that event, ensure "Event parm1 contains recipient" is checked

AnkurBawiskar_1-1744685587203.png

 

AnkurBawiskar_2-1744685664118.png

 

3) use business rule to trigger the event

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var recipient = '';
    var instanceName = gs.getProperty('instance_name');
    if (instanceName == 'dev instance')
        recipient = 'groupSysIdForDev';
    else if (instanceName == 'test instance')
        recipient = 'groupSysIdForTest';

    gs.eventQueue('event_name', current, recipient);

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

10 REPLIES 10

SasiChanthati
Giga Guru

In ServiceNow, making email notifications dynamic based on the environment (Dev, Test, Prod, etc.) is doable. Let me walk you through the basic approach, especially since you're still getting familiar with the platform.

send email notifications to different groups depending on the environment your instance is running in. For example:

- If you're in Dev, send to `SNOW Dev`
- If you're in Test, send to `SNOW Test`
- If you're in Prod, send to a different group (`SNOW Prod`, for example)


Step-by-Step Guide to Dynamically Set Notification Recipients

Option 1: Use a Script in the Notification’s "Who Will Receive" Tab

1. Go to the notification record (type “Notifications” in the left-hand filter navigator).
2. Open your notification.
3. In the "Who will receive" tab, click “New" and select “Script".
4. In the script field, write a script to add recipients based on the environment.

Here’s a sample script:


// Get the current instance name
var instanceName = gs.getProperty('instance_name');

// Decide which group to notify based on the environment
var groupName;

if (instanceName.indexOf('dev') !== -1) {
groupName = 'SNOW Dev';
} else if (instanceName.indexOf('test') !== -1) {
groupName = 'SNOW Test';
} else {
groupName = 'SNOW Prod'; // Default to Prod group or another group
}

// Look up the group
var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('name', groupName);
groupGR.query();
if (groupGR.next()) {
// Add all users in the group as recipients
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('group', groupGR.sys_id);
userGR.query();
while (userGR.next()) {
template.recipients.push(userGR.user.toString());
}
}

This script checks the instance name (e.g., `dev12345.service-now.com`) and sends the notification to the appropriate group.

 

How to Check Your Instance Name?

To test what your `instance_name` is, you can go to System Properties > System and look at `glide.installation.instance`. Or in a Script Include or background script:


gs.print(gs.getProperty('instance_name'));

Bonus Tip: Use System Property for Environment

You can also create a custom property (e.g., `x_custom.environment`) and use that to manage environments explicitly, so you're not relying on the instance name.

Sasi this is beyond helpful, thank you for your time. Follow up question, if you don't mind. Under my 'Who will receive' tab, there is no 'New' button to click. Although, there is I believe a related list at the bottom, that has a tab called 'Script Actions'? Is that the same? 

 The “Script Actions" in the related list is indeed related, but it's not exactly the same as the "New" button in the "Who will receive" tab. Let me clarify the two concepts:

Understanding 'Who Will Receive' vs. 'Script Actions'

1. Who Will Receive Tab:
This is where you specify the recipients for the email notification. Typically, you can use things like:
- Specific users
- Groups
- Roles
- Dynamic conditions via scripts.

When you create a new condition, you're essentially specifying who should receive the email based on specific criteria or using scripting for more dynamic cases.

2. Script Actions Tab:
This is a related list that allows you to define scripted actions that run when the notification is triggered. The Script Action is a more advanced way to customize the behavior of notifications, and it's typically used for more complex requirements like running custom scripts that modify how the notification works or how recipients are determined.


The "New" Button in 'Who Will Receive':

If you don't see the New button in the "Who will receive" tab, it may be because of the following reasons:

- User Role/Permissions: Your user role might not have the right permissions to add new recipients in that tab. Check with your admin to see if you have the correct permissions or if they can add this for you.
- Interface Mode: If you're in a specific view or mode (like List or Form view), sometimes certain options might not be visible. Ensure you are viewing the Form and not just the List.

 

How to Use Script Actions (this is optional)

You can use Script Actions if you need to create dynamic or advanced logic, such as:
- Determining recipients based on certain conditions that aren’t directly accessible in the standard “Who will receive" options.
- Sending emails to different groups based on custom data or instance properties. As an example of how you might use a Script Action to dynamically send an email to different groups based on the environment:

1. Go to the Notification Record.
2. Scroll down to the Related Lists, and find the Script Actions.
3. Add a new Script Action. Here's an example script you might add:


(function execute(inputs, outputs) {
// Get the current instance name
var instanceName = gs.getProperty('instance_name');

// Choose the recipient group based on the environment
var recipientGroup;
if (instanceName.indexOf('dev') !== -1) {
recipientGroup = 'SNOW Dev';
} else if (instanceName.indexOf('test') !== -1) {
recipientGroup = 'SNOW Test';
} else {
recipientGroup = 'SNOW Prod'; // Default group
}

// Logic to send the email to the appropriate group (you may need to adjust this)
var grGroup = new GlideRecord('sys_user_group');
grGroup.addQuery('name', recipientGroup);
grGroup.query();
while (grGroup.next()) {
// Send email to all users in the group
var usersInGroup = new GlideRecord('sys_user_grmember');
usersInGroup.addQuery('group', grGroup.sys_id);
usersInGroup.query();
while (usersInGroup.next()) {
// Here you would trigger the email to the users.
// Example: gs.eventQueue('your.notification.event', current, usersInGroup.user);
}
}
})(inputs, outputs);

This is similar to what we did for Who Will Receive but in a Script Action, giving you flexibility for advanced scenarios.

Who will receive allows you to define who will receive the email in a straightforward way.
Script Actions let you script more advanced behaviors, like dynamic recipient logic or processing actions.

If you don't see the New button in the Who will receive tab, I would suggest you to check permissions or switching to a different mode (form vs. list) might solve the issue. If needed, you can also implement more complex recipient logic through Script Actions.

Hello again Sasi! 

Thank you again for all this information! After asking around, admin says I have the permissions I need and nothing can be added at the moment. So, it looks like I will have to take the scripting route you mentioned with the script action in the related list. If there are any other suggestions you have to make this process easier, please let me know! Again, this has been very helpful