Find all records without active children records?

Shane J
Tera Guru

My question is more specific to Request Item and children Catalog Tasks, but the logic would probably apply to any parent/child or one/many relationship.

I'd like to setup a scheduled job to auto-close any RITMs that don't have any active children Catalog Tasks, but I'm not sure how to query to find the records.   We don't assign RITMs to groups in our environment, so if somehow all of the active Catalog Tasks get closed without the parent RITM getting closed, the RITM just hangs out there in an active state.

I suppose another wrench could be that a RITM could be awaiting an Approval, but have no active Catalog Task.  

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You can do the following to check if your GlideRecord object holds any records.



var inc = new GlideRecord('incident');


inc.addActiveQuery();


inc.query();


inc.HasNext(); //returns true or false depending on whether the inc object has any records.


View solution in original post

10 REPLIES 10

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You'd do a gliderecord query and iterate through all of the active items. Within each item loop you'd do a query on the approval and catalog task tables to see if there are any open records and if not close the item.


How do you run a query to look for 0 records though?


before you start the query for the children set a tag to true...



inside the loop looking for kids set the tag to false..



when the loop ends <after the } to end the while> test the tag.. if it is true it has no children and you can close it...




be careful how you do this though....



if you have an item pending item approval it will be active with no children... if you have an item that creates a change request.. it will probably have no children...



if you have an item that invokes some kind of script in the background.. for example an install of softare or spin up of a   server.. it will be active and have no children.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

You can do the following to check if your GlideRecord object holds any records.



var inc = new GlideRecord('incident');


inc.addActiveQuery();


inc.query();


inc.HasNext(); //returns true or false depending on whether the inc object has any records.