How to call notification from UI Macro ?

Abhijit Das7
Tera Expert

Hi Everyone,

 

I have created UI macro and notification in my instance. And I want to call notification in UI Macro.

 

My UI macro , creates a button on form and when it is clicked then a table pops up and I select a record from table. The details of selected records get populated in additional comments.

 

So, my task is to send notification when I a record is selected. So , I am planning to call notification through UI Macro when record is selected.

 

UI Macro:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<script>
function macroLoading(){
const btn = document.createElement("button");
btn.innerHTML = "CareAR Instruct";
btn.id = "customButton";
btn.type="button";
document.getElementById("element.sn_customerservice_case.watch_list").appendChild(btn);
document.getElementById("customButton").addEventListener("click", myFunction);
//return false;
}
function myFunction(){

var table = 'x_care3_carear_fsm_instruct_experience';
var tableList = 'x_care3_carear_fsm_instruct_experience_list';


var dialog = new GlideDialogForm('Instruct Experience' ,tableList);
dialog.setTitle(table + " List");
dialog.setLoadCallback(populateCaseComments);
dialog.render();

function populateCaseComments(item) {
var fields = ["instruct_model_name", "instruct_model_url", "model_description"];
var msg = "";
fields.forEach(function(field) {
msg += item.getElementById(table + "." + field).value + "\n";
});

g_form.setValue("comments", msg);
if (item.getElementById('sys_uniqueValue').value)
dialog.destroy();

}

}

window.onload = macroLoading;
</script>
</j:jelly>

 

Thanks and regards

cc: @Ankur Bawiskar @kamlesh kjmar 

 

1 ACCEPTED SOLUTION

kamlesh kjmar
Mega Sage
Mega Sage

Hi @Abhijit Das7 ,

 

To call a notification from UI Macro on select of any record, follow the below steps :

 

1. Create an Event record in Event registry module

kamleshkjmar_0-1666873181841.png

 

 

2. Create a notification in Notifications module and trigger when event (event that you created in step1) is fired:

kamleshkjmar_1-1666873303474.png

 

3. Make sure you define a valid recipient in Who will receive sectionFor this example I have kept it system administrator, you can make it dynamic by passing recepient from the event parm1/parm2

kamleshkjmar_2-1666873387136.png

 

4. Create a script include to call an event. It should be client callable so that you can call it from UI Macro, below a sample script.

var notify = Class.create();
notify.prototype = Object.extendsObject(AbstractAjaxProcessor, {
notifyEndUser : function(){
	var record = this.getParameter("record"); //sys_id of the record against which event is triggerd, passing this from UI Macro
	var table = this.getParameter("table"); // Table name against whose record event will be triggered, passing this from UI Macro
	var getRecord = new GlideRecord(table);
	getRecord.get(record);
	gs.eventQueue("notify.user", getRecord, "", ""); //Triggering the event, you can pass the dynamic recepient in second last or last parameter
},
    type: 'notify'
});

 

 

kamleshkjmar_4-1666873532353.png

 

5. Open your UI Macro and call this script include to trigger the event which will eventually trigger your notification:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<script>
function macroLoading(){
const btn = document.createElement("button");
btn.innerHTML = "CareAR Instruct";
btn.id = "customButton";
btn.type="button";
document.getElementById("element.sn_customerservice_case.watch_list").appendChild(btn);
document.getElementById("customButton").addEventListener("click", myFunction);
//return false;
}
function myFunction(){

var table = 'x_care3_carear_fsm_instruct_experience';
var tableList = 'x_care3_carear_fsm_instruct_experience_list';


var dialog = new GlideDialogForm('Instruct Experience' ,tableList);
dialog.setTitle(table + " List");
dialog.setLoadCallback(populateCaseComments);
dialog.render();

function populateCaseComments(item) {
var fields = ["instruct_model_name", "instruct_model_url", "model_description"];
var msg = "";
fields.forEach(function(field) {
msg += item.getElementById(table + "." + field).value + "\n";
});

g_form.setValue("comments", msg);
if (item.getElementById('sys_uniqueValue').value){
            dialog.destroy();
		var eventTrigger = new GlideAjax("notify");
		eventTrigger.addParam("sysparm_name","notifyEndUser");
		eventTrigger.addParam("table",table);
		eventTrigger.addParam("record",item.getElementById('sys_uniqueValue').value);
		eventTrigger.getXML(callEvent)
		function callEvent(response) {  
        var answer = response.responseXML.documentElement.getAttribute("answer"); 
        alert("Event triggered chech your notification log ");
}
		}

}

}

window.onload = macroLoading;
</script>
</j:jelly>

 

kamleshkjmar_5-1666874094889.png

You can check the event log to validate if your event triggered:

kamleshkjmar_6-1666874229654.png

 

Check emails log to validate, if notification triggered

kamleshkjmar_7-1666874284919.png

 

 

 

Please let me know if this do not works.

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh

 

View solution in original post

5 REPLIES 5

Could you please send the screen shot of whom to send section of your notification. Also could you please confirm, once your event is triggered can you see your event name on Event log

 

kamleshkjmar_0-1667458052842.png