Notification subscription

sravanip
Tera Contributor

Hi

I am working on a requirement.

 

AFFECTED MARKET is present in the related list of the incident. when affected market is added to the Incident we have to cross check if there is any subscriptions present in sys_notif_subscription table for respective market. 

  the market value is not populated in INC record. it will be save in m2m table. 

 

so we have an existing event which triggers whenever an inc is updated. adding to this we will have to check if there is any matching subscription for the affected market and trigger the email for the subscribed user.

 

I am stuck here in loop. Looking for effective workaround. 

2 REPLIES 2

Tejas Adhalrao
Mega Sage

hi @sravanip  ,

Since the affected market is stored in an M2M table and not directly on the Incident record, you can query the M2M table first to get the markets associated with the Incident. Then use those market sys_ids to find matching subscriptions in sys_notif_subscription.

You can follow this approach:

  1. Query the Incident-to-Affected Market M2M table using the current Incident sys_id.

  2. Loop through the related records and retrieve the affected market sys_id.

  3. Query sys_notif_subscription for subscriptions matching that market.

  4. For each matching subscription, retrieve the subscribed user and trigger the notification event.

  5. Use a map/object to track processed subscription or user sys_ids and avoid sending duplicate notifications if multiple markets match the same subscription.

Since you already have an event triggered when the Incident is updated, you can add this logic to the existing processing or call a Script Include to keep the implementation reusable.

One thing to watch for: if the Business Rule runs on every Incident update, make sure the notification logic only executes when needed; otherwise, users may receive duplicate emails.

 

If this helped, mark it helpful and accept the solution.

Regards, Tejas

 

Thanks for the input.
solution 1 
I tried BR on m2m table. got market value and pulled matching notification subscription records and user list. Here 

 

var incidentGR = new GlideRecord('incident');
if (!incidentGR.get(current.u_task)) return;
var marketSysId = current.getValue('u_market');
var sub = new GlideRecord('sys_notif_subscription');
sub.addQuery('condition', 'LIKE', 'u_affected_marketsLIKE' + marketSysId);
sub.query();
while (sub.next()) {
var users = sub.user.toString();
gs.eventQueue('incident.create', incidentGR, users,'Update');
}
event is triggered and and in users I see respective email but notification is sent as empty receipt list.   I tried same in advanced condition in notificaiton (which triggers in incident table)

var incidentId = current.getUniqueValue();
var m2m = new GlideRecord("u_m2m_tasks_markets");
m2m.addQuery("u_task", incidentId);
m2m.query();
var notifiedUsers = {};
while (m2m.next()) {
var marketId = m2m.getValue("u_market");
if (!marketId) continue;
var sub = new GlideRecord("sys_notif_subscription");
sub.addQuery("condition", "CONTAINS", "u_affected_markets");
sub.addQuery("condition", "CONTAINS", marketId);
sub.query();
while (sub.next()) {
var userId = sub.getValue("user");
if (!userId || notifiedUsers[userId]) continue;
notifiedUsers[userId] = true;
var user = new GlideRecord("sys_user");
if (user.get(userId) && user.getValue("active") == "true") {
var userEmail = user.getValue("email");
if (userEmail) {
email.addAddress("to", userEmail);
}
}
}
}
answer = true; both send empty emails