Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Modify provider notification before sending

Mike Hashemi
Kilo Sage

I have a provider notification ([sys_notification]) that runs against the [live_notification] table and on record change, insert, and when the field name is work_notes or comments (category is 'connect'). When someone sends an @mention, the recipient gets the notification in their "bell". When they click the notification, instead of being taken to the task (I have been testing with [sn_customerservice_case]) from which they were mentioned, the [live_notification] record is loaded.

 

I noticed that, if I modify the related [ui_notification_inbox] record, changing the target_table from [live_notification] to [sn_customerservice_case], then the notification loads the case. After creating a BR on the [ui_notification_inbox] table (runs before insert, when the trigger is my provider notification), I can see the BR script running, but the target_table is not being updated.

 

My assumption is that, when the record is inserted, BRs do not run, so I had a look at the existing [live_notification] BRs and found one called "Send Notification", which looks like it must create the [ui_notification_inbox] record, but it uses an API for which I cannot find any documentation. I updated the script, but it did not change how the [ui_notification_inbox] record is created.

 

Anyone know how to control [ui_notification_inbox] records or how to make the notifications point to my desired record?

 

Here is the "Send Notification" BR script:

(function executeRule(current, previous /*null when async*/) {

	var profile = current.profile;
	if(!profile) {
 	    profile = new GlideappLiveProfile().getIDFromProfileOrUser(current.user);
	    if (!profile)
            return;
	    current.setValue("profile", profile);
		current.update();
	}
	
	var liveGroupProfileGR = new GlideRecord("live_group_profile");
	liveGroupProfileGR.setWorkflow(false);
	liveGroupProfileGR.addQuery("document", current.document);
	liveGroupProfileGR.addQuery("table", current.table);
	liveGroupProfileGR.addQuery("type", "!=", "support");
	liveGroupProfileGR.query();

	if(liveGroupProfileGR.next()) {
		var liveGroupMemberGR = new GlideRecord("live_group_member");
		liveGroupMemberGR.setWorkflow(false);
		liveGroupMemberGR.addQuery("group", liveGroupProfileGR.getUniqueValue());
		liveGroupMemberGR.addQuery("member", profile);
		liveGroupMemberGR.addQuery("state", "!=", "inactive");
		liveGroupMemberGR.query();

		if(liveGroupMemberGR.next()) {
			return;
		}
	}

	// The following section was added to allow @mention notifications to trigger a notification that points to the task record instead of the live_notification record.
	gs.info('[Notification Update] Running update script in Send Notification BR.');
	var documentGr = current.document.getRefRecord();
	if (documentGr) {
		gs.info('[Notification Update] Found the live_notification\'s document record: ' + documentGr.getUniqueValue());

		var targetTable = documentGr.sys_class_name;

		if (targetTable) {
			gs.info('[Notification Update] Document table: ' + targetTable);

			//current.table = targetTable;
			//current.document = documentGr.getUniqueValue();
		} else {
			gs.info('[Notification Update] No document table returned.');
		}
	}

	new GlideNotificationBroadcaster().broadcast({
		title: current.title,
		message: current.message,
		table: targetTable, //current.table,
		document: documentGr.getUniqueValue() //current.document
	}, profile);

})(current, previous);

I can see it runs by checking the system log and the targetTable/documentGr records are what I expect.

0 REPLIES 0