How to set To field for email dynamically?

Utkarsha
Tera Contributor

Hello All,

I need to set email receipients dynamically from a system property...I have stored email address in the system property

and I want to set them as email recipients using email script.....I am using below code...but it's not working

could anyone suggest me what can be the mistake here?

Email script calling in the notification-

// Fetch the system property
var emailUsers = gs.getProperty('users_to_notify');
// Split the property into an array of email addresses
var emailArray = emailUsers.split(',');
// Set the recipients in the email object
email.addAddress(emailArray.toString());
 
Any kind of support is highly appreciated!
Thank you
1 ACCEPTED SOLUTION

@Utkarsha  

why not include recipient in event parm1 and then combine what you want to send as 1 piece in json in event parm2?

you can define your own json structure for event parm2 and use the same while parsing in email script

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

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Utkarsha  

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

why not have this code where you are using eventQueue based approach and set the recipients from there?

If you are not using eventQueue approach then use that and you can use the recipients from system property

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

Hello @Ankur Bawiskar ,

Thank you for your response on this....I am using the eventQueue based approach but already passing two important parameters ....I don't think I can add one more...or can I ?

below is my code-

gs.eventQueue('custom_event', null, JSON.stringify(UsedTables), JSON.stringify(tableInfo));
Thank you

@Utkarsha  

why not include recipient in event parm1 and then combine what you want to send as 1 piece in json in event parm2?

you can define your own json structure for event parm2 and use the same while parsing in email script

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

piyushsain
Tera Guru
Tera Guru

Hi @Utkarsha  

You can not set To email via email script, on cc and bcc can be set, please send the To recipient email id via an event

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain