- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2021 06:05 PM
We have order guide requests from the past that did not close properly because the RITM stages were never set to completed in the workflow. The workflows have since been fixed.
I'm thinking the script would need to check that all tasks associated with an RITM are inactive, then set the RITM stage to completed. I'm hoping that this can be done without sending out notifications when the request closes.
If the RITM stage is set to complete, the OOB business rule should trigger and close out the parent request.
Can anyone help with the script, or maybe provide a better alternative?
Thank you,
Solved! Go to Solution.
- Labels:
-
Multiple Versions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2021 08:57 PM
Glad to know that my script helped.
You can additional filter conditions for your query to only select particular catalog item's RITM
updateRecords();
function updateRecords(){
try{
var itemNames = 'Catalog Item1,Catalog Item2'; // give your catalog item names here
var ritm = new GlideRecord("sc_req_item");
ritm.addActiveQuery();
ritm.addEncodedQuery("stage!=complete");
ritm.addEncodedQuery('cat_item.nameIN', itemNames);
ritm.query();
while(ritm.next()){
//check for active sc tasks
var taskRec = new GlideRecord('sc_task');
taskRec.addActiveQuery();
taskRec.addQuery("request_item",ritm.sys_id);
taskRec.query();
if(!taskRec.next()){
//close requested item
ritm.stage = "complete";
ritm.active = false;
ritm.state = 3; // set to close complete
ritm.setWorkflow(false); // stop any BR
ritm.update();
//close request
var request = ritm.request.getRefRecord();
request.active = false;
request.state = 3;
request.setWorkflow(false);
request.update();
}
}
}
catch(ex){
gs.info('Exception'+ex);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2021 08:58 PM
// Get Requested which are active and not completed stage
var Ritem = new GlideRecord("sc_req_item");
Ritem.addEncodedQuery("active=true^stage!=complete");
Ritem.query();
while(Ritem.next()){
//check for active sc tasks
var sctask = new GlideRecord('sc_task');
sctask.addEncodedQuery("request_item="+Ritem.sys_id+"^active=true");
sctask.query();
if(!sctask.next()){
//close requested item
Ritem.stage = "complete";
Ritem.active = false;
Ritem.setWorkflow(false); // will supress any scripts/notifications running becoz of this update
Ritem.update();
//close request
var request = Ritem.request.getRefRecord();
if (request){
request.active = false;
request.setWorkflow(false);
request.update();
}
}
}
Try this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2021 09:18 PM
Hi,
below script should help
updateRecords();
function updateRecords(){
try{
var ritm = new GlideRecord("sc_req_item");
ritm.addActiveQuery();
ritm.addEncodedQuery("stage!=complete");
ritm.query();
while(ritm.next()){
//check for active sc tasks
var taskRec = new GlideRecord('sc_task');
taskRec.addActiveQuery();
taskRec.addQuery("request_item",ritm.sys_id);
taskRec.query();
if(!taskRec.next()){
//close requested item
ritm.stage = "complete";
ritm.active = false;
ritm.state = 3; // set to close complete
ritm.setWorkflow(false); // stop any BR
ritm.update();
//close request
var request = ritm.request.getRefRecord();
request.active = false;
request.state = 3;
request.setWorkflow(false);
request.update();
}
}
}
catch(ex){
gs.info('Exception'+ex);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2021 03:26 PM
Thank you, both! The scripts are what I asked for and worked very well, however I found that it closed some requests that shouldn't have been closed due to the way those workflows are setup. Ex. Some workflows don't have any active tasks, but are awaiting approval.
Is there a way to limit this script to only query and close those within a specific order guide or item? If it's possible to limit by opened date as well, that would be amazing. I'm afraid this is too broad, and I'll end up closing requests that should remain open.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2021 08:57 PM
Glad to know that my script helped.
You can additional filter conditions for your query to only select particular catalog item's RITM
updateRecords();
function updateRecords(){
try{
var itemNames = 'Catalog Item1,Catalog Item2'; // give your catalog item names here
var ritm = new GlideRecord("sc_req_item");
ritm.addActiveQuery();
ritm.addEncodedQuery("stage!=complete");
ritm.addEncodedQuery('cat_item.nameIN', itemNames);
ritm.query();
while(ritm.next()){
//check for active sc tasks
var taskRec = new GlideRecord('sc_task');
taskRec.addActiveQuery();
taskRec.addQuery("request_item",ritm.sys_id);
taskRec.query();
if(!taskRec.next()){
//close requested item
ritm.stage = "complete";
ritm.active = false;
ritm.state = 3; // set to close complete
ritm.setWorkflow(false); // stop any BR
ritm.update();
//close request
var request = ritm.request.getRefRecord();
request.active = false;
request.state = 3;
request.setWorkflow(false);
request.update();
}
}
}
catch(ex){
gs.info('Exception'+ex);
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader