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.

Update of multiple records using glide record script

Harry Campbell2
Mega Guru

Hello All,

I am trying to do a cleanup of some older RITM's that are effectively broken due to some workflow issues a few years back.

I have the following script that will find any RITM's that are approved but have no tasks:

var noTaskCounter = 0;

var gr = new GlideRecord('sc_req_item');  

gr.addEncodedQuery('active=true^approval=approved');

gr.query();  

while (gr.next()) {      

      var tsk = new GlideRecord('sc_task');  

tsk.addQuery('request_item', gr.sys_id);

      tsk.query();  

      if (!tsk.next()) {  

              gs.info('RITM == {0} has Zero Tasks',gr.number);  

noTaskCounter++;  

      }  

}  

gs.info('Out of {0} RITMs, only {1} had Zero Tasks', gr.getRowCount(), noTaskCounter);

I can print the list of RITM's and manually update the records but there are a lot and would rather do this via a script.

What do I need to add into this script to update all the RITM's? Initially I will just be setting them to active=false

Many Thanks

Harry

1 ACCEPTED SOLUTION

vinothkumar
Tera Guru

Hi Harry,



It is so simple, just add the gr.update in your script, it will update. Try something as below.


if (!tsk.next()) {      


              gs.info('RITM == {0} has Zero Tasks',gr.number);    


        gr.active =false;


        gr.update();



          noTaskCounter++;      


      }


View solution in original post

6 REPLIES 6

vinothkumar
Tera Guru

Hi Harry,



It is so simple, just add the gr.update in your script, it will update. Try something as below.


if (!tsk.next()) {      


              gs.info('RITM == {0} has Zero Tasks',gr.number);    


        gr.active =false;


        gr.update();



          noTaskCounter++;      


      }


Harry Campbell2
Mega Guru

Hi Vinoth,



I have tried that and came here because I thought I was being stupid - it's not updating the records, it's still just printing the numbers.



Thanks


Harry


Shishir Srivast
Mega Sage

Hi Harry,



Please check if this works



  1. var noTaskCounter = 0;    
  2. var gr = new GlideRecord('sc_req_item');      
  3. gr.addEncodedQuery('active=true^approval=approved');    
  4. gr.query();      
  5. while (gr.next()) {          
  6.       var tsk = new GlideRecord('sc_task');      
  7.       tsk.addQuery('request_item', gr.sys_id);    
  8.       tsk.query();      
  9.       if (!tsk.next()) {      
  10.               gs.info('RITM == {0} has Zero Tasks',gr.number);
  11.               gr.active = false;
  12.               gr.update();  
  13.               noTaskCounter++;      
  14.       }      
  15. }      
  16. gs.info('Out of {0} RITMs, only {1} had Zero Tasks', gr.getRowCount(), noTaskCounter);  

Harry Campbell2
Mega Guru

I didnt use gr.update();



Now i feel like a fool



Thanks for your help