Jon G1
Kilo Sage

I ran into an interesting problem that I couldn't find a great answer for.  When creating a notification rule, you can add alternate SMS text and add users to the "Who will receive", but from everything I can see, SMS is always disabled in user preferences by default.  Maybe there's some simple answer to this that I overlooked.  But since it frustrated me so much, I thought I would share with the community.

So here's the case: When a user gets added to a group that has the group type "SMS Notification", users should be automatically set up for SMS notifications on whatever notification rules point to that group.  Here's the business rule I came up with.  I don't think it's super pretty, and may be more work than necessary, but it functions well.

This is a business rule on 'sys_user_grmember' that watches for inserts/updates when group.type contains "SMS Notification"

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

		var grSmsDevice = new GlideRecord('cmn_notif_device');
		grSmsDevice.addQuery('user', current.user.sys_id);
		grSmsDevice.addQuery('type', 'SMS');
		grSmsDevice.addQuery('active', true);
		grSmsDevice.query();

		//prevent user from being added to SMS group if they do not have an SMS notification device
		if (!grSmsDevice.next()) {
			gs.addErrorMessage(current.user.name + ' does not have a valid SMS notification device - please add one before adding to this group');
			current.setAbortAction(true);
		}
		else {
			//Check which notifiation rules refer to this group
			var grNotificationRule = new GlideRecord('sysevent_email_action');
			grNotificationRule.addEncodedQuery('recipient_groupsLIKE' + current.group.sys_id + '^active=true');
			grNotificationRule.query();

			//if we find notification rules referring to this group, check if the user has an SMS notification subscription to that notification
			while (grNotificationRule.next()) {

				var grSubscription = new GlideRecord('cmn_notif_message');
				grSubscription.addQuery('user', current.user.sys_id);
				grSubscription.addQuery('notification', grNotificationRule.sys_id);
				grSubscription.query();

				var noSMSSubscription = true;
				var noEmailSubscription = true;

				//if they already have a subscription, set SMS notifications to true and clear filter
				while (grSubscription.next()) {
					if (grSubscription.device.type == 'SMS') {
						noSMSSubscription = false;
						grSubscription.send_sms = true;
						grSubscription.send_email = false;
						grSubscription.notification_filter = '';
						grSubscription.update();
						gs.addInfoMessage(current.user.name + ' - ' + grNotificationRule.name + ' rule: SMS ENABLED');
					}
					//disbale email notifications - SMS group notifications should be SMS only
					else if (grSubscription.device.type == 'Email') {
						noEmailSubscription = false;
						grSubscription.send_sms = false;
						grSubscription.send_email = true;
						grSubscription.notification_filter = getFilter();
						grSubscription.update();
						gs.addInfoMessage(current.user.name + ' - ' + grNotificationRule.name + ' rule: EMAIL DISABLED');
					}
				}

				//if user doesn't already have any subscription, create a new SMS notification subscription for the user
				if (noSMSSubscription) {
					var grSubscribeNow = new GlideRecord('cmn_notif_message');
					grSubscribeNow.initialize();
					grSubscribeNow.device = grSmsDevice.sys_id;
					grSubscribeNow.name = grNotificationRule.name;
					grSubscribeNow.notification = grNotificationRule.sys_id;
					grSubscribeNow.send_email = false;
					grSubscribeNow.send_sms = true;
					grSubscribeNow.user = current.user.sys_id;
					grSubscribeNow.insert();
					gs.addInfoMessage(current.user.name + ' - ' + grNotificationRule.name + ' rule: SMS ENABLED');
				}

				if (noEmailSubscription) {
					//check if user has an email device - if so, create an unsubscribe rule
					var grEmailDevice = new GlideRecord('cmn_notif_device');
					grEmailDevice.addQuery('user', current.user.sys_id);
					grEmailDevice.addQuery('type', 'Email');
					grEmailDevice.addQuery('active', true);
					grEmailDevice.query();

					if (grEmailDevice.next()) {
						var grUnsubEmail = new GlideRecord('cmn_notif_message');
						grUnsubEmail.initialize();
						grUnsubEmail.device = grEmailDevice.sys_id;
						grUnsubEmail.name = grNotificationRule.name;
						grUnsubEmail.notification = grNotificationRule.sys_id;
						grUnsubEmail.send_email = true;
						grUnsubEmail.send_sms = false;
						grUnsubEmail.user = current.user.sys_id;
						grUnsubEmail.notification_filter = getFilter();
						grUnsubEmail.insert();
						gs.addInfoMessage(current.user.name + ' - ' + grNotificationRule.name + ' rule: EMAIL DISABLED');
					}
					else {
						gs.addInfoMessage('No email device found for user: ' + current.user.name);
					}
				}
			}
		}
	}
	catch (er) {
		gs.addErrorMessage('BR ERROR: User Added to SMS group - Please notify your system administrator with this entire message\n' + er);
	}
})(current, previous);

function getFilter() {
	var grFilter = new GlideRecord('notification_filter');
	grFilter.get('name', 'Unsubscribe');
	return grFilter.sys_id;
}

So - We check whether the user actually has an SMS notification device.  If not, we stop there.  This user can't receive SMS notifications.

If the user does have an SMS notification device we check all notification rules that send messages to this group.  Does the user already have any notification message preferences set for this notification rule?  If so, enable SMS and disable email.  If not, create the notification message preferences as appropriate.

I struggled with this a bit, so I figured I'd share it for whoever needs a similar solution down the road.

Version history
Last update:
‎01-18-2022 07:10 PM
Updated by: