Background (fix) script to set the RITM stage to Completed when all associated tasks are no longer active

MB12
Kilo Sage

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,

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

6 REPLIES 6

VigneshMC
Mega Sage
// 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

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

MB12
Kilo Sage

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.

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader