Please let me know the correction in the script

BanuMahalakshmi
Tera Contributor

Hi,

Please let me know the correction in the below script? this script works only if I deactivate the user one by one. But this script didn't work, if I deactivate multiple users at a time into list view(by dragging many user list - then set active is false). Thanks.

 

Business Rule:

var userSysId = current.sys_id.toString();
    var approverarray = [];
    var index = -1;
    var ciname;
    var level1approver = new GlideRecord('cmdb_ci');
    level1approver.addQuery('u_level_1_approver', 'CONTAINS', userSysId);
    level1approver.query();
    while (level1approver.next()) {
        var approver = level1approver.getValue('u_level_1_approver');
        approverarray = approver.split(',');
        index = approverarray.indexOf(userSysId);
        ciname = level1approver.getValue('name');

        if (index > -1) {
            approverarray.splice(index, 1);
            gs.eventQueue("trigger.event", level1approver, ciname.toString(), approverarray);
        }
    }
 
Event Registry: triggered into the table cmdb_ci 
 
Script Action:
    var eventParam1 = event.parm1;
    var eventParam2 = event.parm2;

    gs.error('Script Action Triggered');
    gs.info("the paramert1=" + eventParam1);
    gs.info("the parameter2=" + eventParam2);
   
    var level1approver = new GlideRecord('cmdb_ci');
    level1approver.addQuery('name', '=', eventParam1);
    level1approver.query();
    while (level1approver.next()) {

        level1approver.u_level_1_approver = eventParam2.join(,);
        level1approver.update();

    }
BanuMahalakshmi_0-1737473305511.png

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@BanuMahalakshmi 

I could see multiple messages in log which says the BR is running for more than 1 record when you are updating from list view

If your BR triggers multiple times based on those number of records selected it means issue is in eventQueue()

in 4th parameter of eventQueue() you should send recipient

Are you sure in your notification you have set Event parm2 contains recipient checkbox?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@BanuMahalakshmi 

why are you using script action to update the CI?

you can directly update within the IF condition for index check

I hope your BR is after update when user is deactivated

var userSysId = current.sys_id.toString();
var approverarray = [];
var index = -1;
var ciname;
var level1approver = new GlideRecord('cmdb_ci');
level1approver.addQuery('u_level_1_approver', 'CONTAINS', userSysId);
level1approver.query();
while (level1approver.next()) {
    var approver = level1approver.getValue('u_level_1_approver');
    approverarray = approver.split(',');
    index = approverarray.indexOf(userSysId);
    ciname = level1approver.getValue('name');

    if (index > -1) {
        approverarray.splice(index, 1);
        level1approver.u_level_1_approver = approverarray.toString();
        level1approver.update();
    }
}

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

@BanuMahalakshmi 

Hope you are doing good.

Did my reply answer your question?

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

Thanks for reply, Initially i created only business rule to remove deactivated users name from CMDB_CI table. 

But if i deactivate multiple users into List View(drag and select many users then change active = false), these business rule didnt work.

 

So that Architect advised to do trigger event to remove disable user.

BanuMahalakshmi_0-1737529788282.png