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

vinitha3
Tera Guru

Hi,


In scripts, you can send notifications using events.



1. You need to create an email notification separately.


2. you need to create a new event and register the event for sending the email created above.



This is how you can trigger the event:


gs.eventQueue('event.name', recordObject, event.parm1, event.parm2);



where:


event.name is the name of the event you want to trigger.


recordObject is the record you are calling the event against. Many times this is current which is the current record, but can vary.


event.parm1 is an optional parameter you can use. This can be used to store other details such as an alternate recipient.


event.parm2 is an optional parameter you can use.



Please follow this thread: how to send an email notification to a list of members from a group based on condition



Please mark helpful if it had helped you.



Thanks,


Vinitha.K


Ok, So i created the Event and it looks like this...



find_real_file.png



And the notification looks like this....but I'm not sure how to link the event to the notification.   Under 'When to Send' there is no field to specify the Event Name.



find_real_file.png



Also in the Inbound Email Action, I'm calling the event by the following line...



gs.eventQueue("Update User Test", gr, gr.user_id("3a5754b64f0ade00cdf248f18110c7b7"));



How does this all look?


Refer the below screenshot and select your event in place of the event mentioned in the picture.Also, it is good to follow the naming of event as shown in screenshot ,something like user.update.test




And what is the parameter gr.user_id("3a5754b64f0ade00cdf248f18110c7b7") ? i think gr.user_id should be fine.Not sure what exactly you want to pass there .


find_real_file.png


rjp
Tera Expert

This is what my notification looks like, i don't have that 'Send When' field in your screen shot.



find_real_file.png


Are you in advanced view , if you cannot see the field , right click ,edit form layout and add the field from the available list on the left to the right .