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.

how to add email recipients via email script and according to conditions

susunow
Tera Contributor

Hi,

 

I would like to create a schedule email for change task ticket. However, the email template is all using the same one but it is based off of different locations and these locations have different email recipients. 

 

Is there a way for me to do this via email script similar to adding cc email?

 

Thanks 

5 REPLIES 5

Bhavya11
Kilo Patron
Kilo Patron

Hi @susunow ,

 

yes you can, just i want to know is location information present on change task or change record 

if it is present in change task and if your email notification on change task then you can script as below

 

Create system property like below  locations_email_map

{ "abcd1234abcd1234abcd1234abcd1234": "ny-team@company.com", "wxyz5678wxyz5678wxyz5678wxyz5678": "sf-team@company.com", "mnop5678mnop5678mnop5678mnop5678": "london-team@company.com" }

 

 

in email script write this

 

var locationSysId = current.u_location; // if it is reference field 

// Get the location-email map from the system property
var locationEmailMap = gs.getProperty('locations_email_map');
var locationEmailObject = JSON.parse(locationEmailMap);
email.setTo("default.email@company.com");
var recipientFound = false;
for (var sysId in locationEmailObject) {
    if (locationSysId == sysId) {
  
        email.addAddress("cc", locationEmailObject[sysId]);
        recipientFound = true;
        break;  
    }
}

 

 

 

 Thanks,

BK

susunow
Tera Contributor

Hi thank you for this. However, im not looking for the cc but recipient (to) emails. Thanks

 

susunow
Tera Contributor

Hi thank you for this. However, im not looking for the cc but recipient (to) emails. Thanks

 

Hi @susunow 

 

you cannot set the recipients via email script. Only CC and BCC is allowed.

 

if you want to do , in business rule trigger event send the email address and enable  recipient in event parm1 then it will work fine

 

example BR script:

 

var locationSysId = current.u_location ? current.u_location.toString() : '';

var locationEmailMap = gs.getProperty('locations_email_map');
var locationEmailObject = JSON.parse(locationEmailMap);  // Parse the JSON string to an object


for (var sysId in locationEmailObject) {
    if (locationSysId === sysId) {  
                gs.eventQueue('event.name', current, locationEmailObject[sysId]);
        

    }
}