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.

Configure Notifications through Secondary email

rishabh31
Mega Sage

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

 

1 ACCEPTED SOLUTION

BharathChintala
Mega Sage

@rishabh31 

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);
If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

View solution in original post

3 REPLIES 3

BharathChintala
Mega Sage

@rishabh31 

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);
If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

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.

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);