script to trigger Event to remove disable user

BanuMahalakshmi
Tera Contributor

Hi,

 

we keep the business rule for when a user becomes deactivated, and instead of the business rule updating the CI's level 1 approver group
(to remove member), trigger an event passing the CI and the disabled user... and the event logic will update the level 1 approver field to remove disabled user.

 

 var userSysId = current.sys_id.toString();
    var approverarray = [];
    var index = -1;
    var ciname;

    var level1approver = new GlideRecord('cmdb_ci');
    level1approver.addQuery('assigned_to', 'CONTAINS', userSysId);
    level1approver.query();
    while (level1approver.next()) {
        var approver = level1approver.getValue('assigned_to');
        approverarray = approver.split(',');
        index = approverarray.indexOf(userSysId);
        ciname = level1approver.getValue('name');

        gs.eventQueue("Remove.inactive.users.from.Approver.fiel", current, ciname.toString(), approverarray);
        if (index > -1) {
            approverarray.splice(index, 1);
            level1approver.setValue('assigned_to', approverarray.join(','));
            level1approver.update();
        }
    }
5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@BanuMahalakshmi 

what's your actual requirement?

assigned_to on cmdb_ci is reference field

Why in script you are doing split on it?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Assigned_to field type is List (customized field).

@BanuMahalakshmi 

so that deactivated user should be removed from list field and that field should get updated again?

script is fine

what's not working?

event, notification and BR is on which table?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

event is not triggered, event created with cmdb_ci table.

script is fine,  4 users were listed in the assigned_to field, and all 4 users were deactivated at the same time.
In the end, two of the deactivated users ended up staying in the assigned_to field. Performance/locking issue... 

To avoid this issue, instead of the business rule updating assigned_to field,trigger an event passing the CI and the disabled user.

in the above script, not triggering event.