GlideRecord "while (gr.next())" is not going past

vkiran
Mega Expert

Hi All,

I'm unable to glide past the While(gr.next())

I'm unable to get into the If() condition. Any help is much appreciated. Thanks

My code:

(function executeRule(current, previous /*null when async*/) {

gs.log('R1');

// Add your code here

var gr = new GlideRecord('problem');

var cnew = current.u_problem_id.getRefRecord();

gr.addQuery('sys_id',cpNew);

cnew.getValue('number');

gr.addQuery('number',cnew.getValue('number'));

gs.log(cnew.getValue('number') + '1.2');

gr.query();

gs.log('R2');

while(gr.next())

{

if(gr.problem_state == 7)

{

current.state = 3;

current.update();

}

else

{

current.state = 15;

current.update();

}

}

gs.log('Relationship 6');

})(current, previous);

1 ACCEPTED SOLUTION

sanjay36
Tera Expert

Hey Venkat ,




You have a code error in line it should be as below :




var gr = new GlideRecord('problem');  


gr.addQuery('sys_id',current.u_problem_id);  


gr.query();  


if(gr.next())    


{  


if(gr.problem_state == 7)  


{  


current.state = 3;  


current.update();  


}  


else  


{  


current.state = 15;  


current.update();  


}  


}


View solution in original post

3 REPLIES 3

sanjay36
Tera Expert

Hey Venkat ,




You have a code error in line it should be as below :




var gr = new GlideRecord('problem');  


gr.addQuery('sys_id',current.u_problem_id);  


gr.query();  


if(gr.next())    


{  


if(gr.problem_state == 7)  


{  


current.state = 3;  


current.update();  


}  


else  


{  


current.state = 15;  


current.update();  


}  


}


Patrick Schult2
Giga Guru

Check your line 7 - the "cpNew" variable doesn't appear to have been declared anywhere.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Venkat,



From the code it looks like this BR should do something like: if the associated problem state is 7 set the current state to 3, else set it to 15. Assuming that u_problem_id is a reference field, you can do it a little simpler.



(function executeRule(current, previous /*null when async*/ ) {


      // Add your code here



      if (current.u_problem_id.problem_state == 7) {


              current.state = 3;


      } else {


              current.state = 15;


      }



})(current, previous);



Also assuming this is running in a business rule you should make it a before business rule and get rid of the current.update(). I would also make sure that you're using some sort of changes() condition so it doesn't run on every db update.