Pls advise script action not trigger into the line indexof

BanuMahalakshmi
Tera Contributor

Hi,

I have attached system log, the business rule fetching CI sys id and current user id(from user table). 

 

event triggered, script action. but the script execution failed into the line indexof. index value printing -1 still though it has value. kindly let me know the correction in the script:

 

Business Rule:

(function executeRule(current, previous /*null when async*/) {
   var userSysId = current.sys_id.toString();
    var civalue;
    var gr1 = new GlideRecord('cmdb_ci');
    gr1.addQuery('u_level_1_approver', 'CONTAINS', userSysId);
    gr1.query();
    while (gr1.next()) {
        civalue = gr1.sys_id;
        gs.log("the ci value=" + civalue);
        gs.log("the user sysid=" + userSysId);
        gs.eventQueue("remove.user", gr1, civalue, userSysId);
    }
})(current, previous);
 
 Event Trigger: into CMDB_CI table
 
Script Action:
gs.log(event.parm1);
gs.log(event.parm2);

deactivateuser(event.parm1, event.parm2);

function deactivateuser(id, username) {
    var approverarray = [];
    //var index = -1;
    var gr = new GlideRecord("cmdb_ci");
    gr.addQuery('sys_id', id);
    gr.query();

    while (gr.next()) {
       
        var approver = gr.getValue('u_level_1_approver');
        approverarray = approver.split(',');
        gs.log("the approver array=" + approverarray);
        gs.log("the username=" + username);
        var index = approverarray.indexOf(username);
        gs.log("index=" + index);
        if (index != -1) {
            approverarray.splice(index, 1);
            gs.log("the array=" + approverarray);
            gr.u_level_1_approver = approverarray;
            //  gr.setValue('u_level_1_approver', approverarray.join(','));
            gr.update();
        }
    }
}
BanuMahalakshmi_0-1737969316819.png

 

1 ACCEPTED SOLUTION

function deactivateuser(id, username) {
    var gr = new GlideRecord("cmdb_ci");
    gr.addQuery('sys_id', id);
    gr.query();

    while (gr.next()) {
        var approver = gr.getValue('u_level_1_approver');
        if (!approver) continue; // Safeguard for empty fields
        var approverarray = approver.split(',').map(function(item) {
            return item.trim().toLowerCase();
        });
        
        username = username.trim().toLowerCase();

        gs.log("Approver Array: " + approverarray);
        gs.log("Username: " + username);

        var index = approverarray.indexOf(username);
        gs.log("Index: " + index);

        if (index !== -1) {
            approverarray.splice(index, 1);
            gr.setValue('u_level_1_approver', approverarray.join(',')); // Convert back to string
            gr.update();
            gs.log("Updated Approver Array: " + approverarray);
        }
    }
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

5 REPLIES 5

@BanuMahalakshmi 

Thank you for marking my response as helpful.

As pew new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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