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

Script to find any RITM's that have no catalog tasks

Harry Campbell2
Mega Guru

Hello All,

I need a background script that will find any active approved RITM records that have no catalog tasks.

In our system, all approved requested items should have a catalog task but there are a lot of historical records that have none. I need to find all of these records.

I think a background script is the best way to achieve this but I'm not sure how it would work. I have a script that will find and print any RITM numbers that are active and approved:

var gr = new GlideRecord('sc_req_item');

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

gr.query();

while (gr.next()) {

gs.print(gr.number);

}

Is there anything I can add to this script to return the RITM numbers that have no catalog tasks?

Many Thanks

Harry

1 ACCEPTED SOLUTION

Patrick DeCarl1
ServiceNow Employee
ServiceNow Employee

Harry,



Try below script.



var noTaskCounter = 0;


var gr = new GlideRecord('sc_req_item');


gr.addEncodedQuery('active=true^approval=approved'); //<-- whatever you want to filter on


gr.query();


while (gr.next()) {


      //you could do a GlideAggregate here also.


      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);  



Output ex:


*** Script: RITM == RITM0000012 has Zero Tasks


*** Script: Out of 8 RITMs, only 1 had Zero Tasks


View solution in original post

6 REPLIES 6

Harry Campbell2
Mega Guru

Correct me if i'm wrong but if there are no tasks for an RITM then running the query on the sc_task table wont work - because the tasks dont exist.



I need a list of all RITM records that dont have any catalog tasks.


Not sure who you're replying to, but basically you want to loop through every request item in your instance and then check to see if it has any related tasks, which you do by query the sc_task table. If it doesn't have tasks then you would print the number or add it to an array of item numbers or something like that.