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

Try this, you will certainly need to make some modifications but here is a basic framework and I have added comments to show where sys_id is fund and where it is not found

 

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]) > 0) {
//sys_id found
//do whatever
}
else
{
//sysId not found
                gr.u_comments = "" + "Responsiblity for account " + current.company.name + " and contact " + current.contact.name + " has changed to:" + authorityrole.toString();
}
            }
        }

 

-Anurag

Not applicable

@Anurag Tripathi I am getting below error now:

java.sql.BatchUpdateException: (conn=2399162) Duplicate entry 'bob@coastal.com' for key 'user_name'

Where are you updating anything? 

-Anurag

Not applicable

Hi @Community Alums ,

 

You are splitting with comma & then checking the index Of. Rather first iterate over the arrays using for loop to & then check indexof inside the loop.

 

for (var j = 0; j < pasa.length; j++)

{

if (authrole.indexOf(pasa[j]) !== -1)

{

 

I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.

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.