Create fix script to insert device for user

gracjan
Tera Guru

Please assist. We have users that have email address but no device notification entry. We want to create a device record from currently existing email from user table with cmn_notif_device table. We want to use a fix script to accomplish that. Thank you for any help.

 

device.JPG

name.JPG

4 REPLIES 4

Hemanth M1
Giga Sage
Giga Sage

Hi @gracjan ,

 

Create a fix script as below.

var id = "6816f79cc0a8016401c5a33be04be321"; //update sys_id of the user you want to create the device

//get user details
var usr = new GlideRecord("sys_user");
usr.get(id); 

//insert device details
var usrCom = new GlideRecord("cmn_notif_device");
usrCom.initialize();
usrCom.setValue("name", "Primary email");
usrCom.setValue("type", "Email");
usrCom.setValue("email_address", usr.email);
usrCom.insert();

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Community Alums
Not applicable

Hello @gracjan ,

 

Below is an example of a fix-script that retrieves user_id and email from the sys_user table and filters out those with email IsNotEmpty, checks if there are no corresponding records in the cmn_notif_device table where user_id = recently fetched user_id from the sys_user table, and creates a device record:

// Glide the sys_user table to fetch users with non-empty emails
var userGr = new GlideRecord('sys_user');
userGr.addQuery('email', '!=', '');
userGr.query();

while (userGr.next()) {
    var userId = userGr.getValue('user_id');

    // Check if there is no record in cmn_notif_device for the current user_id
    var notifDeviceGr = new GlideRecord('cmn_notif_device');
    notifDeviceGr.addQuery('user', userId);
    notifDeviceGr.query();

    if (notifDeviceGr.getRowCount() === 0) {
        // If no record is found, create a new device record in cmn_notif_device
        var newDevice = new GlideRecord('cmn_notif_device');
        newDevice.initialize();
        newDevice.setValue('user', userId);
        newDevice.setValue('email', userGr.email); // Assuming email field in sys_user
        newDevice.insert();
    }
}

Make sure to adjust field names ('user', 'email') in the script according to your ServiceNow instance configuration if they differ from the assumed field names.

 

Please correct me if I have misunderstood your requirement.

 

If you found this helpful, a 'like' is the secret handshake of appreciation! Accept it as a solution, as it will help others find the correct solution quickly.

 

- Prasad 😉

I mad few modifications to the code and it worked:

var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('employee_numberSTARTSWITH12029596^user_nameISNOTEMPTY');
userGr.query();

while (userGr.next()) {
    var userId = userGr.getValue('sys_id');

    // Check if there is no record in cmn_notif_device for the current user_id
    var notifDeviceGr = new GlideRecord('cmn_notif_device');
    notifDeviceGr.addQuery('user.sys_id', userId);
    notifDeviceGr.query();

    if (notifDeviceGr.getRowCount() === 0) {
        // If no record is found, create a new device record in cmn_notif_device
        var newDevice = new GlideRecord('cmn_notif_device');
        newDevice.initialize();
        newDevice.setValue('user', userId);
		newDevice.setValue('name', 'Primary email');
		newDevice.setValue('type', 'Email');
		newDevice.setValue('primary_email', 'true');
        newDevice.setValue('email_address', userGr.email); // Assuming email field in sys_user
        newDevice.insert();
    }
}

 

Community Alums
Not applicable

@gracjan, Fantastic news!😄

 

Accept it as a solution, as it will help others find the correct solution quickly.

 

- Prasad