Auto close the parent record when child tasks are closed.

Nagashree5
Tera Contributor

I have a record producer that creates a parent Service desk record and auto creates 3 child general tasks. I would like to prevent from 'Closed Complete' the parent Service desk record until all the child general tasks are 'Closed Complete'. 

Can someone please help with this.

6 REPLIES 6

Kamil Smusz
Kilo Sage

Hi,

 

You need Before Update Business Rule.

KamilSmusz_0-1679669109543.png

 

Below code example for sc_req_item table.

 

(function executeRule(current, previous /*null when async*/) {
var sc_count = [];
	var sctasks = new GlideRecord('sc_task');
    sctasks.addQuery('request_item', current.sys_id);
    sctasks.addQuery('state', 'IN', '-5,1,2');
    sctasks.query();

    while (sctasks.next()) {

        sc_count.push(sctasks.number);

    }
	
    if (sc_count.length == 1) {

		gs.addErrorMessage('There is '+sc_count.length + " open task. Request can't be closed!");
        current.setAbortAction('true');
    }
	if (sc_count.length > 1) {

		gs.addErrorMessage('There are '+sc_count.length + " open tasks. Request can't be closed!");
        current.setAbortAction('true');
    }


})(current, previous);

 

 

VaishnaviShinde
Kilo Sage

Hi @Nagashree5 

 

Solution 1:

Create before update Business rule and condition in that state Changes to closed complete.

Add the below code in script section. Replace the table name and fields with your table.

    var scTask = [];
    var grTaskCount = new GlideAggregate('sc_task');
    grTaskCount.addQuery('request_item', current.getUniqueValue());
    grTaskCount.addAggregate('COUNT');
    grTaskCount.query();
    if (grTaskCount.next()) {
        var count = grTaskCount.getAggregate('COUNT');
        var grTask = new GlideRecord('sc_task');
        grTask.addQuery('request_item', current.getUniqueValue());
        grTask.addEncodedQuery('state=3'); // add this query if you want to check only closed complete
        //grTask.addEncodedQuery('stateIN3,4,7'); // add this query if you want to check closed complete incomplte or sckipped
        grTask.query();		
        while (grTask.next()) {
            scTask.push(grTask.getUniqueValue());
        }		
		if(count != scTask.length){
			gs.addErrorMessage('Please close all the tasks before close the Request');
			current.setWorkflow(false);
			current.setAbortAction(true);
		}
    }

 

Solution 2: If you are using flow then only use below solution.

If you have flow for this, then add the lookup Records action for sctask table at last, add the condition like Request item is trigger records sys id and then add if block lookup record count is greater than 0 and then add wait for action add the condition like wait till all tasks get close and the add update Record action in that update the RITM record to close.

 

If my answer solved your issue, please mark my answer as Correct & Helpful

Hi @VaishnaviShinde ,

 

Thanks for the response. 

Here, I want to close the request when the related child tasks are closed. The Request state will be read-only and it will follow the task states.

Can you please elaborate on the solution2.

 

Thank you in Advance.

Hi @Nagashree5 

Below is example of RITM

Add all the below action at the end of flow.

1) Add the below lookup records action. In table field add your child table name.

VaishnaviShinde_0-1679817627745.png

2) After that add below if condition 

VaishnaviShinde_1-1679817746237.png

3) In then block add below foreach loop 

VaishnaviShinde_2-1679817851246.png

4) Then add the below wait for condition

VaishnaviShinde_3-1679817926983.png

5) Add Update record action for you parent table.

VaishnaviShinde_4-1679817984759.png

 

The sequence of steps should be as below image. 

VaishnaviShinde_5-1679818063266.png

 

If my answer solved your issue, please mark my answer as Correct & Helpful