how to trigger an event from a UI Action button

sonbol_montakha
Giga Contributor

I have a send button , by clicking the send button an email should be sent to some people. I've already set up an email notification , an event, and a business rule. But my question is that how do I trigger the event with my UI action button ( send)?

1 ACCEPTED SOLUTION

Only if you are using the parameters as recipients should you choose to check these.


View solution in original post

19 REPLIES 19

ccajohnson
Kilo Sage

You can trigger an event directly from the UI Action. If your UI Action has a client-side and server-side code, make sure the eventQueue call is in the server-side portion of your code. The eventQueue is broken up in the following way:



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.



For more information, see the following Wiki article:


Events and Email Notification - ServiceNow Wiki


Thanks Christopher,


Actually My business rule function is as follows:


function onAfter(current, previous) {


    if (current.operation() == 'insert')


    gs.eventQueue("send_email", current, event.param1, event.param2);



}



so , I suppose based on your answer, I should write in the UI action script rather than the business rule??


The parameters are there so you can call them within the notification if you choose. Please keep in mind that both the event as well as the notification need to be on the same table otherwise the notification will not be sent.



Also, looking at your code, you are not defining the event parameters at all and would probably get an error when you try to execute your code. I would try your code within a background script first to determine if you are triggering the event properly. For my example I am going to send an event called 'sample.event' to an Incident record with a sys_id of '03015a872922f100e093d33a2261c55c' assigning the first parameter as the name of the current user and 'Sample Subject' as the second parameter. After you elevate your privileges to security_admin, and launch the Scripts - Background module, I run the following script:



var current = new GlideRecord('incident');


current.get('03015a872922f100e093d33a2261c55c');


gs.eventQueue('sample.event', current, gs.getUserName(), 'Sample Subject');



This will trigger the event on the incident table and show the user name of myself in the first parameter and 'Sample Subject' as the second parameter.



In the notification, I use ${event.parm2} in the subject line to show 'Sample Subject'. I also use Triggered by: ${event.parm1} to show who the user name is of who triggered the event.



It is unclear if you are using the parameters for anything that you cannot get from the current record. If you are, then you need to get that information within the UI Action before putting it into the eventQueue syntax.



Take a look at the sample syntax that ServiceNow uses in the following Wiki article:


Events and Email Notification - ServiceNow Wiki


Actually I though by checking event param 1 and event param2 , the code would be executed!! Am I correct?


find_real_file.png