Send Email In UI Action

stevejarman
Giga Guru

Can someone point me in the direction of how to send an email directly from a UI Action script? Sorry if I'm missing something obvious, but I haven't found this documented as yet.

As an example, on a Change Request, I want to add a UI Action that gives me a "Start Work" button. When the owner of the CR hits that button, I'll (amongst other things) have the UI Action script send an email to the change advisory board to let them know the CR is starting. I essentially want the equivalent of Javascript:

SendEmail("changeboard@somewhere.com", "This is the subject", "This is the HTML body");

Or

SendEmail("changeboard@somewhere.com", UsingThisTemplate);

Is there such a thing, or something along those lines?

1 ACCEPTED SOLUTION

While not as flexible as a notification, you can hard code an email via script that can be called by a UI action. Below is an example:



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


View solution in original post

9 REPLIES 9

What if I need to add parameters in the email body, to include the change summary, change start time, ...


Also, for the recipients how get input for it


Ramez, the script provided was an example and so to add more than just "email body" to the email you just change it to:


var emailNotification = new GlideRecord('sys_email');


emailNotification.type = 'send-ready';


emailNotification.subject = 'Email subject';


emailNotification.body = 'Change Summary: ' + current.description + '\n' + 'Change Start: ' + current.work_start;


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


emailNotification.insert();



Notice I included a '\n' which makes the next set of text on a new line.   You can keep adding new fields using the + and this is nothing special about ServiceNow this is a standard JavaScript format.


Thank you,


Can we get the recipients email addresses from watch list ?


The watch list contains a comma separated list of user SysID's.   So you would need to use a script to loop through them to compile the list of email addresses.   This isn't tested but something like the following should work using my earlier example code:


//Get Watch List User's Email Addresses


var watchListEmails = [];


var userRec = new GlideRecord("sys_user");


userRec.addQuery("sys_id", "IN", current.watch_list.toString());


userRec.query();


while (userRec.next()) {


      watchListEmails.push(userRec.email + "");


}



var emailNotification = new GlideRecord('sys_email');


emailNotification.type = 'send-ready';


emailNotification.subject = 'Email subject';


emailNotification.body = 'Change Summary: ' + current.description + '\n' + 'Change Start: ' + current.work_start;


emailNotification.recipients = watchListEmails.toString() + email-adress1 + ',' + email-address2;


emailNotification.insert();


Hello Michael;


I have the same requirement in which i have to send an email to the 3rd party if the sub-category is meet the requirement. I created an button in the form so that when we click that button an email is sent to the 3rd party having the details about the user . I used the same script above but i think that is not working . Please me out with this.


Thank you