Get state value of child tasks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 04:52 AM - edited 03-25-2024 04:54 AM
Hello,
I have 2 child tasks created under a parent record. And two tasks are of two different “Type”s. Now I wan to get the state value of those two tasks.. how can I get that?
based on the state values of those two tasks, I need to set field on parent record. It’s not just when two child tasks state is closed then close parent.. we have other combinations we need to check for state fields on both child tasks..
when state on two child tasks are closed then close parent.
when state on one task is closed and other one is pending then set state on parent to different value.
Any help with script is much appreciated!
Thanks,
Ben
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 04:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 05:01 AM
Hi Jadadish,
It’s our custom tables but Consider them as Incident and Incident task table.
thanks,
Ben.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 05:05 AM - edited 03-25-2024 05:11 AM
Hello @Ben42
var child = new GlideRecord('incident');
child.addQuery('parent', current.parent);
child.query();
while (child.next()) {
var type= child.type;
gs.print(type);
}
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 04:57 AM - edited 03-25-2024 05:00 AM
Hi @Ben42,
Please find the below sample code and let me know whether it is useful or not.
(function executeRule(current, previous /* previous is only available in before business rules */) {
// Query for child tasks related to the parent record
var childTaskGr = new GlideRecord('child_task_table_name');
childTaskGr.addQuery('parent', current.parent); // Assuming 'parent' is the field linking child tasks to the parent record
childTaskGr.query();
var allClosed = true;
var atLeastOneClosed = false;
// Loop through child tasks
while (childTaskGr.next()) {
var state = childTaskGr.state; // Assuming 'state' is the field storing the state of the child task
// Check state conditions
if (state != 'closed') {
allClosed = false;
} else {
atLeastOneClosed = true;
}
}
// Update parent record based on conditions
var parentGr = new GlideRecord('parent_table_name');
if (atLeastOneClosed && allClosed) {
parentGr.get(current.parent);
parentGr.state_field = 'Closed'; // Update parent state field
parentGr.update();
} else if (/* other conditions */) {
// Handle other conditions as needed
}
})(current, previous);
Thanks
SP