send email in a script

youchan
Kilo Contributor

In the workflow, I have a "run script" activity. In this, I got an array of emails, managers, and groups. (ie, groups[0] is name of groupA, managers[0] is the name of the manger for groupA, and emails[0] is the email address for the group manager of A). Note that the all 3 arrays have the same length. I also have a user variable, which remains unchanged throughout the workflow.

Now, i want to send am email to each element in the arrays. Something like

for (var i = 0; i < emails.length; i++){

//send email to $emails[i] that says "Hello $managers[i], the user $User_Variable has been added to your group $groups[i]."

}

I don't know how I can achieve this.

I googled this problem and I think event queue is something I can use? But I have no idea how to use event queue.

Any insight is appreciated, thank you.

1 ACCEPTED SOLUTION

Bryan Tay3
Mega Guru

hi Youchan,



u can try this



for (var i = 0; i < emails.length; i++){


//send email to $emails[i] that says "Hello $managers[i], the user $User_Variable has been added to your group $groups[i]."


  var mail = new GlideEmailOutbound();  


  mail.addRecipient($emails[i]);  


  mail.setSubject('Add user Notification');  


  mail.setBody('Hello $managers[i], the user $User_Variable has been added to your group $groups[i].');


  mail.save();


}



You might want to manipulate the variable in the email body like 'Hello ' + managers[i] etc...



Hope this helps.


View solution in original post

3 REPLIES 3

Bryan Tay3
Mega Guru

hi Youchan,



u can try this



for (var i = 0; i < emails.length; i++){


//send email to $emails[i] that says "Hello $managers[i], the user $User_Variable has been added to your group $groups[i]."


  var mail = new GlideEmailOutbound();  


  mail.addRecipient($emails[i]);  


  mail.setSubject('Add user Notification');  


  mail.setBody('Hello $managers[i], the user $User_Variable has been added to your group $groups[i].');


  mail.save();


}



You might want to manipulate the variable in the email body like 'Hello ' + managers[i] etc...



Hope this helps.


thank you this is definitely on the right track / what i was looking for!



I found this right before coming across your response, but i think they are similar - and what i wanted.



var emailNotification = new GlideRecord('sys_email');


emailNotification.type = 'send-ready';


emailNotification.subject = 'Email subject';


emailNotification.body = 'Email body';


emailNotification.recipients = email-adress1 + ',' + email-address2;


emailNotification.insert();


Justin Abbott
Giga Guru

I would use a combination of the following articles to build an email notification that is triggered by an event.



Events and Email Notification - ServiceNow Wiki


http://wiki.servicenow.com/index.php?title=Email_Notifications#gsc.tab=0