need to close RITM after all sc_tasks are completed

Vani14
Tera Contributor

For one RITM, we built two sc_tasks. When I try to close one sc_task, RITM closes but another sc_task remains open. Can somebody assist me in closing all sc_tasks before RITM closes?

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Vani14 

you can use after update BR on sc_task with proper conditions for State value

Condition: State [IS ONE OF] Closed complete, Closed incomplete, Closed skipped

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	
	// check if there is no active task for this RITM
	
	var gr = new GlideRecord("sc_task");
	gr.addActiveQuery();
	gr.addQuery("request_item", current.request_item);
	gr.setLimit(1);
	gr.query();
	if (!gr.hasNext()) {
		var ritm = current.request_item.getRefRecord();
		ritm.state = 3;
		ritm.update();
	}

})(current, previous);

OR

you can also use Flow designer with no script for this

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

@Vani14 

please use script I shared above.

sharing again with some changes

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	
	// check if there is no active task for this RITM
	
	var gr = new GlideRecord("sc_task");
	gr.addActiveQuery();
	gr.addQuery("request_item", current.request_item);
	gr.setLimit(1);
	gr.query();
	if (!gr.hasNext()) {
		var ritm = current.request_item.getRefRecord();
		ritm.state = current.state;
		ritm.update();
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

Rahul Kumar17
Tera Guru

Hi 

 

By creating a Business Rule on the sc_task table that triggers when a task is closed. In the Business Rule, you can check if all the related sc_tasks are closed, and if so, you can close the associated RITM.

Here is an example of a Business Rule script that you can use as a starting point:

// Get the current sc_task and its associated RITM
var currentTask = current;
var currentRITM = currentTask.request_item;

// Get all the sc_tasks associated with the RITM
var taskGr = new GlideRecord('sc_task');
taskGr.addQuery('request_item', currentRITM);
taskGr.query();

// Check if all the sc_tasks are closed
var allTasksClosed = true;
while (taskGr.next()) {
  if (taskGr.getValue('state') != '7') { // '7' is the state value for Closed
    allTasksClosed = false;
    break;
  }
}

// If all sc_tasks are closed, close the associated RITM
if (allTasksClosed) {
  currentRITM.setValue('state', 3); // '3' is the state value for Closed Complete
  currentRITM.update();
}

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

@Vani14 

you can use after update BR on sc_task with proper conditions for State value

Condition: State [IS ONE OF] Closed complete, Closed incomplete, Closed skipped

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	
	// check if there is no active task for this RITM
	
	var gr = new GlideRecord("sc_task");
	gr.addActiveQuery();
	gr.addQuery("request_item", current.request_item);
	gr.setLimit(1);
	gr.query();
	if (!gr.hasNext()) {
		var ritm = current.request_item.getRefRecord();
		ritm.state = 3;
		ritm.update();
	}

})(current, previous);

OR

you can also use Flow designer with no script for this

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Vani14
Tera Contributor

we are using this script is it correct.

 

 

 

(function executeRule(current, previous /*null when async*/) {

var gr=new GlideRecord('sc_task');
gr.addQuery('request_item',current.request_item);
gr.addQuery('state', '!=',current.state);//checking if all tasks are closed
if(!gr.next())

{
var grr = new GlideRecord('sc_req_item');
grr.get(current.request_item);

grr.state=current.state;

grr.update();
}
})(current, previous);

@Vani14 

please use script I shared above.

sharing again with some changes

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	
	// check if there is no active task for this RITM
	
	var gr = new GlideRecord("sc_task");
	gr.addActiveQuery();
	gr.addQuery("request_item", current.request_item);
	gr.setLimit(1);
	gr.query();
	if (!gr.hasNext()) {
		var ritm = current.request_item.getRefRecord();
		ritm.state = current.state;
		ritm.update();
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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