Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

nested for loop and IndexOf not working

Community Alums
Not applicable

HI Team,

I have written Before insert/update BR.Below are the details:

 

u_authority_role --> Its a list collector

 

My requirement is if current value in list collector is not PA even though existing/previous value can be PA, it should not push the values to array -->  authorityrole.push(gr1.name);

The system is getting stuck , its only when I deactivate this it works.

Kindly help!

 

var cuauth_role = current.u_authority_role.toString();
            var cuauthrole = cuauth_role.split(',');
            gs.info('Current aut role:' + cuauthrole);
            var pauth_role = previous.u_authority_role.toString();
            var pauthrole = pauth_role.split(',');
            gs.info('Previous aut role:' + pauthrole);
            for (var i = 0; i < cuauthrole.length; i++) {
                for (var j = 0; j < pauthrole.length; i++) {
                    // gs.info('I am in loop');
                    if (cuauthrole[i] != pauthrole[j] && (pa.indexOf(cuauthrole[i])==-1)) {
                        gs.info('change found');
var gr1 = new GlideRecord('sn_customerservice_responsibility_def');
                        gr1.get(cuauthrole[i]);
                        authorityrole.push(gr1.name);
}
2 ACCEPTED SOLUTIONS

Maik Skoddow
Tera Patron
Tera Patron

Sorry, but your code is smelling (read https://www.opsera.io/blog/what-is-code-smell) and you really should start learning how to write code in a way that is readable and understandable!

 

I absolutely have no idea what you want to achieve, but at least I can tell you why your code is getting stuck. It's due to your bad index variable names in the two for loops.

 

I guess you wanted to write j++ and not i++ in the second loop:

for (var i = 0; i < cuauthrole.length; i++) {
     for (var j = 0; j < pauthrole.length; j++) {

 

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums The first issue I noticed with your script is with the second for loop where you are doing i++ instead it should be j++

 

var cuauth_role = current.u_authority_role.toString();
            var cuauthrole = cuauth_role.split(',');
            gs.info('Current aut role:' + cuauthrole);
            var pauth_role = previous.u_authority_role.toString();
            var pauthrole = pauth_role.split(',');
            gs.info('Previous aut role:' + pauthrole);
            for (var i = 0; i < cuauthrole.length; i++) {
                for (var j = 0; j < pauthrole.length; j++) { //updated to j++
                    // gs.info('I am in loop');
                    if (cuauthrole[i] != pauthrole[j] && (pa.indexOf(cuauthrole[i])==-1)) {
                        gs.info('change found');
var gr1 = new GlideRecord('sn_customerservice_responsibility_def');
                        gr1.get(cuauthrole[i]);
                        authorityrole.push(gr1.name);
}

Also, since you didn't post the complete script, I am not sure if pa variable used at the following line is already defined in the script or not.

if (cuauthrole[i] != pauthrole[j] && (pa.indexOf(cuauthrole[i])==-1)) {

 

Hope this helps.

View solution in original post

2 REPLIES 2

Maik Skoddow
Tera Patron
Tera Patron

Sorry, but your code is smelling (read https://www.opsera.io/blog/what-is-code-smell) and you really should start learning how to write code in a way that is readable and understandable!

 

I absolutely have no idea what you want to achieve, but at least I can tell you why your code is getting stuck. It's due to your bad index variable names in the two for loops.

 

I guess you wanted to write j++ and not i++ in the second loop:

for (var i = 0; i < cuauthrole.length; i++) {
     for (var j = 0; j < pauthrole.length; j++) {

 

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums The first issue I noticed with your script is with the second for loop where you are doing i++ instead it should be j++

 

var cuauth_role = current.u_authority_role.toString();
            var cuauthrole = cuauth_role.split(',');
            gs.info('Current aut role:' + cuauthrole);
            var pauth_role = previous.u_authority_role.toString();
            var pauthrole = pauth_role.split(',');
            gs.info('Previous aut role:' + pauthrole);
            for (var i = 0; i < cuauthrole.length; i++) {
                for (var j = 0; j < pauthrole.length; j++) { //updated to j++
                    // gs.info('I am in loop');
                    if (cuauthrole[i] != pauthrole[j] && (pa.indexOf(cuauthrole[i])==-1)) {
                        gs.info('change found');
var gr1 = new GlideRecord('sn_customerservice_responsibility_def');
                        gr1.get(cuauthrole[i]);
                        authorityrole.push(gr1.name);
}

Also, since you didn't post the complete script, I am not sure if pa variable used at the following line is already defined in the script or not.

if (cuauthrole[i] != pauthrole[j] && (pa.indexOf(cuauthrole[i])==-1)) {

 

Hope this helps.