- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2015 10:02 PM
Hi All,
I have a situation where users were created by a Transform Map without business rules running and therefore without the Create Primary Email Device business rule having been triggered against them. Some users do have Primary Email Devices created (due to being created via a different method which did trigger the BR) but most haven't - so I need to be aware of inadvertently creating duplicate records.
In my dev environment I tried to just activate a copy of the Business Rule for Create Primary Email Device to also fire on Update, after which I ran some updates to the sys_user records containing an email address, but of course this runs the risk of creating duplicates devices which I don't want.
I'm sure this can be done via a Background Script, but I'm struggling to understand the behaviour I need to run in that script. Any advice?
The script needs to do the following:
Look at a sys_user record which contains an Email Address.
For each sys_user record that matches that condition:
- Check the cmn_notify_device table to see if there are any records which are related to that sys_user ID
- If a cmn_notify_device record exists which matches the sys_user ID, then do nothing
- If no cmn_notify_device record exists, and the sys_user record contains an email address
Would appreciate some help here.
Cheers,
Kevin
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2015 11:19 PM
Hi Kevin,
Based on your req, I have written the below script. Please go through it once. Run this on dev/sandbox instance first for testing.
var gr = new GlideRecord('sys_user');
gr.addQuery('emailISNOTEMPTY^active=true');
gr.query();
while(gr.next())
{
var check = cmnRec(gr.sys_id,gr.email);
}
function cmnRec(usrSysId,email)
{
var device = new GlideRecord('cmn_notif_device');
device.addQuery('user', usrSysId);
device.query();
if(!device.next())
{
device.user = usrSysId;
device.email_address = email;
device.primary_email = true;
device.active = true;
device.type = 'Email';
device.name = 'Primary email';
device.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2015 12:14 AM
Nice work Pradeep! Thank you so much! Before I fire it, I'm wondering if there are unintended consequences of having user records with no primary email device given they are intended to be there for all users.
I wonder if it's not more prudent to just create all the missing primary email device records for all the user records that exist? Thoughts guys?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2015 06:58 PM
Pradeep's script did the trick. I'm seeing unexpected behaviour with primary email devices in the Prod environment - They seem to get created on demand when the notification_engine.process event is triggered, but I can't see what is actually doing this. I see Mid Server authentication events triggering around the same time, but It's not clear that these are caused by this or not.
If anyone is aware of any non-business rule activity that might cause a Primary Email Device to be created on-demand when a Notification is sent to a user without an email device, I'd be interested in understanding more about how and why it happens.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2022 12:38 PM
Thanks so much, Pradeep! 6 years later and it worked great for a similar issue we were having. Appreciate it!!! 🙂