- 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-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:40 AM
Pradeep - your script works really well. I've also put together a new business rule that will fire whenever an email address is added to any of the User Records that DON'T have an email device - this one seems to work for me too:
var thisSysID = current.sys_id;
//Check the SysID email is not blank
if (current.email != '') {
//Then count how many Notification Devices have that email address:
var count = new GlideAggregate('cmn_notif_device');
count.addQuery('email_address',current.email);
count.addAggregate('COUNT');
count.query();
while (count.next()) {
var notifMail = count.getAggregate;
var notifMailCount = count.getAggregate('COUNT');
gs.log("The are currently " + notifMailCount + " notification device records with an email address of " + current.email);
}
if (notifMailCount < 1) {
//Write a new device
var device = new GlideRecord('cmn_notif_device');
device.user = thisSysID;
device.email_address = current.email;
device.primary_email = true;
device.active = true;
device.type = 'Email';
device.name = 'Primary email';
//device.insert();
gs.addInfoMessage(gs.getMessage('Primary email device created for') + ' ' + GlideStringUtil.escapeHTML(current.name));
}
}
I think that between the background script and the business rule it will ensure that users with an email address always have a Primary email device.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2015 06:17 PM
Hi Kevin,
Thanks for the feedback.
Can you please mark the response as correct/helpful and close the loop if this answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2016 02:33 PM
HI Pradeep,
This is helpful. We are trying to create a record for a user in cmn_notif_message table, with a device type primary email. But when I insert a record with primary email, it does not appear in the notiication preferences of the user.
I have select the device individually for a user, any idea why?
thanks,