- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 11:16 PM
Dear Team,
can anyone help with the steps to follow on how to configure ServiceNow by using a secondary email for notifications? In other words, I want to use a second email just for notifications and nothing else.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 11:43 PM
Create a business rule on sys_email before insert that adds each recipient's alternate email address to the Recipients field.
(function executeRule(current, previous /*null when async*/) { // get the current recipient(s) var emailTo = current.recipients.split(','); for (var i = 0; i < emailTo.length; i++) { // look for a user matching a recipient email address var userRec = new GlideRecord('sys_user'); userRec.addEncodedQuery('email=' + emailTo[i] + '^u_alternate_emailISNOTEMPTY'); userRec.query(); if (userRec.next()) { // found one, so add their alternate email emailTo.push(userRec.u_alternate_email); } } // save the recipients current.recipients = emailTo.join(','); })(current, previous);
Bharath Chintala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 11:43 PM
Create a business rule on sys_email before insert that adds each recipient's alternate email address to the Recipients field.
(function executeRule(current, previous /*null when async*/) { // get the current recipient(s) var emailTo = current.recipients.split(','); for (var i = 0; i < emailTo.length; i++) { // look for a user matching a recipient email address var userRec = new GlideRecord('sys_user'); userRec.addEncodedQuery('email=' + emailTo[i] + '^u_alternate_emailISNOTEMPTY'); userRec.query(); if (userRec.next()) { // found one, so add their alternate email emailTo.push(userRec.u_alternate_email); } } // save the recipients current.recipients = emailTo.join(','); })(current, previous);
Bharath Chintala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 10:30 PM - edited 04-12-2023 10:31 PM
Thanks @BharathChintala Sir,
This is working fine, just to add in my ask that I have a custom field in my sys_user table named u_secondary_email and per your script just replaced the field name and its working as desired.
Thank you.
Marking helpful & Correct solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 03:35 AM
Thanks @BharathChintala . It gave me a good starting point. As I prefered not to create a custom field like u_alternate_email on the user table (because the list was limited), I stored the alternate emails in a system property and went with the BR as below:
{
"user1@example.com": "alt1@example.com",
"user2@example.com": "alt2@example.com"
}
(function executeRule(current, previous /*null when async*/) {
// Get the JSON string from the system property
var emailMappingJSON = gs.getProperty('email.alternate.mapping');
if (emailMappingJSON) {
try {
// Parse the JSON string into a JavaScript object
var emailMapping = JSON.parse(emailMappingJSON);
// Get the current recipient(s)
var emailTo = current.recipients.split(',');
var updatedRecipients = [];
for (var i = 0; i < emailTo.length; i++) {
var recipient = emailTo[i].trim();
updatedRecipients.push(recipient);
// Add alternate email if mapped and not already added
if (emailMapping.hasOwnProperty(recipient)) {
var alternateEmail = emailMapping[recipient];
if (alternateEmail && updatedRecipients.indexOf(alternateEmail) === -1) {
updatedRecipients.push(alternateEmail);
}
}
}
// Save the updated recipients
current.recipients = updatedRecipients.join(',');
} catch (e) {
gs.log('Error parsing alternate email mapping system property: ' + e.message, 'Alternate Email BR');
}
}
})(current, previous);