Looking for help with a business rule that will change "stage" to "completed"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2023 02:29 PM
I have a business rule (see below) that when all tasks are closed completed then it will close the RITM even when the request is using workflow and an ad hoc task was created. This code is working for when the last SCTASK is closed then it closes the RITM.
The issue is that it's NOT changing the "Stage" to "Completed" It's only changing the "State" to "Close Complete", and I need them both to show as completed. See screenshot below.
(function executeRule(current, previous /*null when async*/) {
// Query to check if there are any open sc_tasks for the current ritm
var task = new GlideRecord('sc_task');
task.addQuery('request_item', current.request_item);
task.addQuery('active', true);
task.query();
var hasActiveTasks = false;
// Check if there are any active tasks
while (task.next()) {
hasActiveTasks = true;
break;
}
if (!hasActiveTasks) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
// Query to check if there are any active tasks remaining for the current ritm
var remainingTasks = new GlideRecord('sc_task');
remainingTasks.addQuery('request_item', current.request_item);
remainingTasks.addQuery('active', true);
remainingTasks.query();
var activeTaskCount = 0;
while (remainingTasks.next()) {
activeTaskCount++;
}
// If no active tasks are remaining, close the ritm
if (activeTaskCount === 0) {
ritm.state = 3; // Closed state
ritm.update();
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 12:48 PM
So there is no way for the code to look at ALL tasks and when ALL tasks have been completed from either an ad hoc task or a workflow task then once all tasks are completed then change the RITM to state=close complete and stage=completed? I thought out of the box you could add an ad hoc task even if the catalog item has workflow with multiple tasks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 01:16 PM
there is , but it cannot look at future tasks
What I mean is::
Lets say on the first step of the workflow you create 2 tasks.
Then these tasks are closed(both), at tnis time yiur BR will see that all tasks for this RITM are closed and close the RITM.
On the Third step there is another task created -> this is an issue in your logic as RITM is closed already.
Only way to get past this is in your workflow, on the very last step wait for all tasks to close and then proceed to close the RITM, what you are gaining here is that there will not be any more tasks created for this, its all already created.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 02:29 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 02:41 AM
Remove the BR, deactivate it.
Your wait for condition should wait for all tasks to close and in the next activity you set state and stage both to complete.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 09:50 AM