We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

IndexOf not working in Business Rule

Not applicable
HI Team,
 
In Below script IndexOf is not working. its always getting passed and control is going beyond IF loop even I ave out condition as   if (authrole.toString().indexOf(pasa) == -1)  .. This means if record is not found it should go but its going even if its not found. I want to stop this/
 
var authorityrole = [];
    var gr = new GlideRecord('customer_contact');
    gr.addQuery('sys_id', current.contact);
    gr.query();

    while (gr.next()) {
        var pa = gs.getProperty('sn_customerservice.PASA');
        var pasa = pa.split(',');
        gs.info('PASA:' + pasa);


        if (current.u_authority_role.changes()) {

            var auth_role = current.u_authority_role.toString();
            gs.info('AuthRole:' + auth_role);
            var authrole = auth_role.split(',');
            if (authrole.toString().indexOf(pasa) == -1) {
                gs.info('insideIF');
                for (var i = 0; i < authrole.length; i++) {
                    gs.info('Into Res:');
                    // gs.info('Got PASA:');
                    authorityrole.push(authrole[i]);
                }

                gr.u_comments = "" + "Responsiblity for account " + current.company.name + " and contact " + current.contact.name + " has changed to:" + authorityrole.toString();
            }
        }
1 ACCEPTED SOLUTION

Not applicable

This is the complete code. 

var authorityrole = [];
var gr = new GlideRecord('customer_contact');
gr.addQuery('sys_id', current.contact);
gr.query();

while (gr.next()) {
    var pa = gs.getProperty('sn_customerservice.PASA');
    var pasa = pa.split(',');
    gs.info('PASA: ' + pasa);

    if (current.u_authority_role.changes()) {
        var auth_role = current.u_authority_role.toString();
        gs.info('AuthRole: ' + auth_role);
        var authrole = auth_role.split(',');

       
        var found = false;

        for (var i = 0; i < pasa.length; i++) {
            if (authrole.indexOf(pasa[i]) !== -1) {
                found = true;
                authorityrole.push(pasa[i]);
            }
        }

        if (found) {
            gs.info('insideIF');
            gr.u_comments = "Responsibility for account " + current.company.name + " and contact " + current.contact.name + " has changed to: " + authorityrole.toString();
        }
    }
}

 

View solution in original post

10 REPLIES 10

Not applicable

This is the complete code. 

var authorityrole = [];
var gr = new GlideRecord('customer_contact');
gr.addQuery('sys_id', current.contact);
gr.query();

while (gr.next()) {
    var pa = gs.getProperty('sn_customerservice.PASA');
    var pasa = pa.split(',');
    gs.info('PASA: ' + pasa);

    if (current.u_authority_role.changes()) {
        var auth_role = current.u_authority_role.toString();
        gs.info('AuthRole: ' + auth_role);
        var authrole = auth_role.split(',');

       
        var found = false;

        for (var i = 0; i < pasa.length; i++) {
            if (authrole.indexOf(pasa[i]) !== -1) {
                found = true;
                authorityrole.push(pasa[i]);
            }
        }

        if (found) {
            gs.info('insideIF');
            gr.u_comments = "Responsibility for account " + current.company.name + " and contact " + current.contact.name + " has changed to: " + authorityrole.toString();
        }
    }
}