Auto close the parent record when child tasks are closed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2023 07:21 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2023 07:44 AM - edited 03-24-2023 07:45 AM
Hi,
You need Before Update Business Rule.
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2023 09:34 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2023 11:52 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2023 01:09 AM
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.
2) After that add below if condition
3) In then block add below foreach loop
4) Then add the below wait for condition
5) Add Update record action for you parent table.
The sequence of steps should be as below image.
If my answer solved your issue, please mark my answer as Correct & Helpful