The CreatorCon Call for Content is officially open! Get started here.

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

Hi Harry,



You are welcome, sometimes some small things won't be visible for our eyes even how many hours we spent also, while others easily find it out. I also come across such situation many times


Edxavier Robert
Mega Sage

Hi @Harry Campbell , 

 

Where do you run this script? I am looking to do the same to clean up our environment from old request from previous years. 

Do we use the Xplore: Developer Toolkit?

 

BTW - I am new ServiceNow stuff,