Need to write script to close RITM if all the associated multiple tasks are closed

Kishan4
Mega Expert

HI All,

 

We had an issue with few of the RITMs, where RITMs with multiple tasks are not closed even if all their associated catalog tasks are closed. The requirment is to identify all those RITMs by getting them into log  and close them.

 

Note: All those RITMs have multiple tasks each.

1 ACCEPTED SOLUTION

Kach
Giga Contributor

Run this code in a fix script. Mark as helpful if it helped.

 

var ritm = new GlideRecord("sc_req_item");
//check for all active RITM
ritm.addEncodedQuery("active=true");
ritm.query();

while (ritm.next()) {
   
    var task_incomplete = 0;
    var tasks = new GlideRecord('sc_task');
    tasks.addQuery('request_item', ritm.sys_id);
    tasks.query();
    while (tasks.next()) {
        //check is task is ative
        if (tasks.active == true) {
            task_incomplete = 1;
        }
    }
    //close RITM if all tasks are inactive
    if (task_incomplete == 0) {
        //close all RITM with no active Tasks
        ritm.setValue('active', false);
        ritm.update();
    }
}

View solution in original post

9 REPLIES 9

Addy22
Tera Guru

For this you can write a after business rule on update of sc_task table

If task gets completed, it should check state of all sister tasks (All child tasks of the parent).

If all are in completed state, mark RITM complete.

 

Otherwise Scheduled Jobs to check it everyday.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please update script as this

var ritm = new GlideRecord("sc_req_item");
ritm.addActiveQuery();
ritm.query();
while (ritm.next()) {
	var tasks = new GlideRecord('sc_task');
	tasks.addActiveQuery();
	tasks.addQuery('request_item', ritm.sys_id);
	tasks.setLimit(1);
	tasks.query();
	if(!tasks.next()) {
		ritm.setValue('active', false);
		ritm.state = 3;
		ritm.update();
	}
}

Regards
Ankur

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

Shawn Gienow
Tera Guru

I've seen this several times lately, and most of them have the same root cause:

You need to ensure you set the stage to "complete" in a flow, or workflow at the end of the process.   

For Flow Designer, that is update the triggering record, to stage: complete.    For a workflow, that is setting the stage of the workflow.

I had to run a fix script similar to the above.

thanks , are there any steps available which i can use to fix this ?

Which part are you trying to fix?

For existing records, if you open sc_req_item and create a view that shows you all of the records in question (ie: finished, but not "closed complete") you can copy that encoded query into an .addEncodedQuery() statement, then set them to stage = complete (I think that's 3, if I recall correctly, but could be wrong here.. look at the choice list for stage.)

For fixing the root cause, if it is due to incorrect staging, either add the stage "complete" to the end step of your workflow, or use a flow to set the RITM record to stage = complete.