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-12-2023 02:58 PM
Hi Julie,
Stage is controlled by the workflow, but since you are using a BR you need to add a line to update the stage as well
Try this
(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.stage='complete';
ritm.update();
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2023 10:03 PM
Now nothing is working like it was I'm using workflow and trying to to be able to do an ad hoc task. It was working with the above code and now nothing is working. I'm going to submit another question because I need to be able to add an ad hoc task with using workflow and that the BR checks to make sure all tasks are closed before the RITM close complete and Stage is complete.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 12:08 PM
So I added this to the bottom of my code and after the first task from workflow now the RITM changes to state=close complete and state=fulfillment and the next task opens based on the workflow. Issue is I have an open task and a RITM that is closed.
ritm.stage ='completed';
ritm.update();
}
}
}
})(current, previous);
I need a way for the script to see even a next task in workflow and an ad hoc task and don't stage=close complete and state=completed the RITM until ALL tasks are closed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 12:23 PM
You cant do that, a BR Cant see the workflow for the tasks that will be created in future.
If you need to do that then implement this on the workflow only as a a last activity.