GlideRecord "while (gr.next())" doing weird stuff

bennyphipps
Giga Expert

Hi,

I've noticed some weird things while performing a query...   Basically what I want is to provide the end users (with a certain role) with the ability to cancel requests that are pending approval still...   I've done this by creating a UI Action "Cancel request" on sc_req_item table with the script as below:

What is weird is that it doesn't seem to be iterating through the while (approval.next()){ properly.... it's like it's saying approval.next() == false when there's one approval and only going through 1 approval if there are two approvers on the request.

If there are NO approvals it does trigger the gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals"); bit ok though... weird...   If there's only 1 approval it just does nothing.

Anyone got any ideas???

      var doneAppr = "";

  var user = gs.getUserDisplayName();

var approval = new GlideRecord('sysapproval_approver');

      approval.addQuery('sysapproval', current.sys_id);

      approval.addQuery('state', 'requested');

      approval.query();

if(!approval.next()){

  gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals");

} else {

  while (approval.next()) {

  var req = approval.sysapproval.sys_id;

  if(doneAppr.indexOf(req)==-1){

  doneAppr += req;

  approval.state = 'rejected';

  approval.comments = "Cancelled by " + user;

  approval.update();

  gs.addInfoMessage("Request Item: " + current.number + " has been cancelled");

      }//end IF

  }//end While

}//end else

The person who helps find the solution wins a cup of tea (that they have to make themselves sorry).

1 ACCEPTED SOLUTION

Thanks



However a colleague walked past my desk asked why I had a picture of a cup of tea on a forum thread then immediately informed me I was over complicating things...   basically the first if (!approval.next()) bit was skipping the first record hence why for 1 record it gave nothing and for 2 records it only gave one...



the script now looks like this - one thing... how do you insert code into SN forum so i look pretty like yours and not just text like mine



var user = gs.getUserDisplayName();



var approval = new GlideRecord('sysapproval_approver');  


      approval.addQuery('sysapproval', current.sys_id);


      approval.addQuery('state', 'requested');  


      approval.query();  




if(approval.next()){



  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();


  gs.addInfoMessage("Request Item: " + current.number + " has been cancelled");




} else {


  gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals");


}//end else


View solution in original post

4 REPLIES 4

bennyphipps
Giga Expert

Here's an incentive...



Tea.PNG


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Ben,



Here you go.


var doneAppr = "";


var flag = 'false';


var user = gs.getUserDisplayName();


gs.addInfoMessage(user);




var approval = new GlideRecord('sysapproval_approver');


approval.addQuery('sysapproval', 'b017a2244fab7100da197d2ca310c75f');


approval.addQuery('state', 'requested');


approval.query();


while(approval.next())


  {


  flag = 'true';


  var req = approval.sysapproval.sys_id;


  if(doneAppr.indexOf(req)==-1){


  gs.addInfoMessage('test');


  doneAppr += req;


  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();


  }


}


if(flag == 'false');


  {


  gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals");


}






Thanks



However a colleague walked past my desk asked why I had a picture of a cup of tea on a forum thread then immediately informed me I was over complicating things...   basically the first if (!approval.next()) bit was skipping the first record hence why for 1 record it gave nothing and for 2 records it only gave one...



the script now looks like this - one thing... how do you insert code into SN forum so i look pretty like yours and not just text like mine



var user = gs.getUserDisplayName();



var approval = new GlideRecord('sysapproval_approver');  


      approval.addQuery('sysapproval', current.sys_id);


      approval.addQuery('state', 'requested');  


      approval.query();  




if(approval.next()){



  approval.state = 'rejected';


  approval.comments = "Cancelled by " + user;


  approval.update();


  gs.addInfoMessage("Request Item: " + current.number + " has been cancelled");




} else {


  gs.addInfoMessage("Request Item: " + current.number + " cannot be cancelled as there are no active approvals");


}//end else


Hi Ben,



Thanks for the update. While replying you will see the option "Use advanced editor". You have to click on that and paste the script.