sending an email from within a script

drslry2
Tera Contributor

Hi Gurus,

I'm attempting to run a script that updates a table then sends an email after the update.

The update of the table works fine, however the sending of the email does not.

The lines in bold below do not appear to be working properly.  

Can anyone tell me how to send an outbound email directly from a script?

Any info will be appreciated.

Thanks in advance!

var user = email.body.employee.toString();

var gr = new GlideRecord('sys_user');

gr.addQuery('email',user);  

gr.query();

gs.log(gr.getRowCount());

if (gr.next())

{

        gr.active = false;

        gr.update();

email.setFrom("umassmeddev@service-now.com");

email.setReplyTo("robert.poschmann@umassmed.edu");

email.setSubject("This is the new subject line");

email.setBody("This is the new body");

}

19 REPLIES 19

JJ1
Kilo Guru

Hello,



The bold highlighted text will work on a mail script , Is this a mail script or business rule ?



You can create a new notification which runs on an event (Create an email notification )



Use gs.eventQueue method in your business rule to trigger the event which will inturn trigger the notifications.You can mention the subject ,To address on the notification record.




Refer this for the event method - How to pass parameter in event


rjp
Tera Expert

Hi JJ,


The script resides in an inbound action email, is there anyway to script the email to run from the inbound action, other than an event that calls a notification?



Thanks again,


Rob


Hi Rob,



Inbound email action is to receive emails from outside in servicenow and then create or update records, it only gets trigger when instance receives new email or email reply. It will not trigger an email notification when any records get updated or inserted, so it won't behave as outbound email notification.



To send the notification based upon the condition you need to create email notification either event based or when the records get updated/created. You can have either email script to customize the other email parameters or use OOB email notification to trigger an email.


You can run the script from inbound action , but as mentioned earlier you need to create a notification which runs on event ,   then trigger the event from inbound action. If the inbound action is updating something you can do some notifications directly from the table where update is done if it meets your purpose.