IndexOf not working in Business Rule

Community Alums
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

Community Alums
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

Anurag Tripathi
Mega Patron
Mega Patron

 

Hi,

What do you get for this info message?

    gs.info('PASA:' + pasa);

-Anurag

Community Alums
Not applicable

comma separated sys_ids

when you split it by the , is becomes an array, pasa is an array now.

When you compare it with Index of you need to use either pasa[0] OR pasa[1] and so on or put it in a loop and use the counter pasa[i]

Try 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(',');
for (var c = 0;c< pasa.length;c++)
{
            if (authrole.toString().indexOf(pasa[c]) == -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();
            }
        }
-Anurag

Community Alums
Not applicable

auth_role  -->Has comma separated sys_ids

pasa -->Has comma Separated sys_ids

 

My requirement is to find any sys_id of pasa is in auth_role. If no ids found then goto For loop ?and add comments else do not go. Will this work?