How to send a notification when a button clikecd

Naga Surendra 1
Mega Expert

Hi,

i have created Ui action (button) and email notification. My requirement is when the user clicked on the button then email should trigger.

Regards,

Surendra.

3 REPLIES 3

Chuck Tomasi
Tera Patron

The easiest way is to trigger an event in the UI action using gs.eventQueue() and have a notification respond to that event.



Reference:


Events and Email Notification - ServiceNow Wiki


Email Notifications - ServiceNow Wiki


zica
Giga Guru

Naga,



These are the steps we took to get this up and running...




I have selected only the fields 'Incident Number', 'Short Description' and 'Causing System' on our Incident table for the example below -



These are the steps to set it up:



1) Go to UI actions, and create a new one for the email button . Enter a script based on the one below - Choose the values you want to add to the email (this is the top half of script - note that some fields require 2 lines of scripting to retrieve the values, if these are a choice/reference field, for example, such as 'u_causing_system') and the popup window (this is the bottom half of the script). Chose the name of your new popup window, and replace 'popup1' with this new name on the line 'var dialog = new GlideDialogWindow("popup1")'


....................................


function sendEmail(){



var incident_number = g_form.getValue("number"); var short_description = g_form.getValue("short_description");


var u_causing_system__value = g_form.getValue("u_causing_system_");


var u_causing_system_ = g_form.getOption('u_causing_system_',


u_causing_system__value).text;



//Initialize and open the Dialog Window



var dialog = new GlideDialogWindow("popup1"); dialog.setSize(1200,1200); dialog.setTitle("MTI Notification Preview - Incident In Progress");



dialog.setPreference("short_description", short_description); dialog.setPreference("incident_number", incident_number); dialog.setPreference("u_causing_system_", u_causing_system_);



dialog.render();



action.setRedirectURL(current);



}


....................................



2) Go to UI Pages and create your new popup window with your chosen title for 'popup1'. Enter a script based on the one below, and amend the values in the HTML script to match those shown in the UI action. The top of the HTML field retrieves the values shown in the UI action, and the bottom half presents these values onto the popup window. You can amend the format of the window here, along with the action buttons visible.



....................................


<?xml version="1.0" encoding="utf-8"?>


xmlns:g2="null">



expression="RP.getWindowProperties().get('short_description')" /> expression="RP.getWindowProperties().get('incident_number')" /> expression="RP.getWindowProperties().get('u_causing_system_')" />



value="${jvar_incident_number}"/>



Incident Title: ${jvar_short_description}


Incident Number: ${jvar_incident_number}


Causing System: ${jvar_u_causing_system_}         id="send_email">Send id="cancel_button">Cancel



....................................


-The Client Script should be:


....................................



function onSubmit() {


//GlideDialogWindow.get().destroy();


return true;


}



function cancel() {


var c = gel('cancelled');


c.value = "true";


GlideDialogWindow.get().destroy();


return false;


}


....................................


-Then scroll down to the 'Processing Script':


....................................



var sysid = '';


var gr = new GlideRecord('incident');


gr.addQuery('number', inc_number);


gr.query();


if(gr.next()){


if (cancelled != "true") {


gs.eventQueue('XXXX', gr, gs.getUserID(), gs.getUserName()); } sysid = gr.sys_id; }



response.sendRedirect('incident.do?sys_id='+sysid);


gs.addInfoMessage('YYYY.');



....................................


-Amend 'XXXX' to be a new event name (eg, 'Email1') for the event to be called. Amend 'YYYY' to be the info message you want to be displayed when the email has been sent.



3) Go to the Registry and create a new event based on this, called XXXX or 'Email1'



4) Go to the Notifications section, and create a new notification based on the new event created in (3), making sure you select 'Event is fired:Email1/XXXX.



That should be it.


Amrit Pal
Giga Contributor

Hi

 

My popup window is not coming.I have used the entire code provided by you but the window to send notification is not coming

 

Do i need to give any condition in onClick() ui Action button. Please respond fast