Script for checking open catalog tasks in a RITM and not close the RITM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 08:58 PM
Hi Community,
I am trying to write a fix script to close the RITM which has all the catalog tasks closed and not close the RITM if it has at least one catalog task open. Please help me troubleshoot, it's not checking the catalog task open when the script is run.
Below is the script I am using -
var Closedcomplete = 0;
var grSCtask = new GlideRecord('sc_task');
grSCtask.addEncodedQuery('active=false'); //<-- whatever you want to filter on
grSCtask.query();
while (grSCtask.next()) {
if(grSCtask.parent != grSCtask.request_item){
grSCtask.parent = grSCtask.request_item;
grSCtask.update();
}
var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('sys_id', grSCtask.request_item);
grRITM.query();
var count = grSCtask.getRowCount();
while(grRITM.next()) {
if(grSCtask.state == '3'){
Closedcomplete += 1;
}
if (Closedcomplete == count) {
grRITM.state = '3';
grRITM.stage = 'complete';
grRITM.setWorkflow(false);
grRITM.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:59 PM
Hello @Varun Sai
Please use this -
var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('state', open_value);
grRITM.query();
var count = grSCtask.getRowCount();
var flag = false;
while(grRITM.next()) {
var grSCtask = new GlideRecord('sc_task');
grSCtask.addEncodedQuery('request_item='+grRITM.sys_id); //<-- whatever you want to filter on
grSCtask.query();
while (grSCtask.next()) {
if(grSCtask.state==value_of_open_state_in_task){
flag=true;
}
}
if(flag==false){
grRITM.state = '3';
grRITM.stage = 'complete';
grRITM.setWorkflow(false);
grRITM.update();
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 10:20 PM
Hi,
So the script you are using is initially iterating through all sc_task records that are closed (active=false)
That way you have no idea if there exists additional records that are open and belong to a RITM that's not completed.
I would suggest a slightly different approach.
First loop through all open RITM records, then for each RITM record found, see if all the attached SCTASK records are closed, and if so, close the RITM record.
A simple example
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addActiveQuery(); // change query to your needs
ritmGR.setLimit(3); // try with a few records to test at first
ritmGR.query();
while (ritmGR.next()){
var sctaskGR = new GlideRecord('sc_task');
sctaskGR.addEncodedQuery('active=true^request_item=' + ritmGR.getUniqueValue()); // find if there exists open task records to this RITM
sctaskGR.query();
if (!sctaskGR.hasNext()){
// no open task records exist
// do your logic to update the RITM record here
// don't forget to complete/cancel existing flow/workflow that runs on the RITM
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 11:34 PM
Hi @Harsh_Deep
Thank you for your response.
Your script is working for me but one issue is there in line #2 when I run the script it's updating all the RITM's which are in open state to closed complete. which will be a huge data update.
grRITM.addQuery('state', open_value);
I made changes and updated it like below with your given script- added comments on where I added.
var grRITM = new GlideRecord('sc_req_item');
grRITM.addencodedQuery('active=true'); // made updates here
//grRITM.addQuery('state', open_value);
grRITM.query();
var flag = false;
while(grRITM.next()) {
var grSCtask = new GlideRecord('sc_task');
grSCtask.addEncodedQuery('active=false^parentSTARTSWITHchg'); //made updates here
grSCtask.addQuery('request_item', grRITM.sys_id); // made updates here
grSCtask.query();
while (grSCtask.next()) {
grSCtask.parent = grSCtask.request_item;
grSCtask.update();
if(grSCtask.state== '2' || grSCtask.state== '-3' || grSCtask.state== '1'){
flag=true;
}
}
if(flag==false){
grRITM.state = '3';
grRITM.stage = 'complete';
grRITM.setWorkflow(false);
grRITM.update();
}
}
I am thinking the line 2 with the active = true is the problem.
I am writing this fix script to update the open RITM's which are stuck open state even when the catalog tasks are closed while checking if all the catalog tasks are closed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 11:40 PM
Hello @Varun Sai
No it will not update all the RITM's,
Because we are setting flag as false
if(flag==false){
So it will be false when it will not have any Closed sc_task related to that RITM
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.