- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2023 01:46 PM
I created this business rule below. I'm using workflow that is creating additional SCTASKS. Then I create an ad hoc SCTASK (which is not in workflow) and if all the workflow SCTASKS are closed and the last ad hoc task is closed it does not close the RITM. Any idea how to get even ad hoc tasks to close the RITM if it's not in the workflow?
==========================BUSINESS RULES============================
Table - Catalog Task (sc_task)
When to Run - After Update
Condition - State is One of Closed States
Script -
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var sct = new GlideRecord('sc_task');
sct.addQuery('request_item', current.request_item);
sct.query();
var totalCount = sct.getRowCount();
var sct1 = new GlideRecord('sc_task');
sct1.addQuery('state', IN, '3,4,7');
sct1.addQuery('request_item', current.request_item);
sct1.query();
var completedtotalcount = sct1.getRowCount();
if (totalCount == completedtotalcount) {
var ritm = current.request_item.getRefRecord();
ritm.state = 3;
ritm.update();
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2023 01:47 PM
Hi @Julie Catano can you please give a try with below script
(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);
Please mark it Correct and Hit Like if you find this helpful!
Regards,
Eswar Chappa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2023 10:10 AM
I added the latest code and then also had to run this BR too. See below
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2023 12:01 AM
Try the below code on sc_task table after update
(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();
// If no active tasks are found, close the ritm
if (!task.next()) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
ritm.state = 3; // Closed state
ritm.update();
}
}
})(current, previous);
Please mark it Correct and Hit Like if you find this helpful!
Regards,
Karthiga
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2023 08:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2023 10:02 AM
What is an ad hoc SCTASK? And how did you create it?
To fix this, you need to check if the SCTASK is in the workflow or not. You can use the workflow field of the SCTASK table to get the workflow ID associated with the task. If the task is not in a workflow, this field will be empty.
Can you try:
!task.hasNext()
instead of this:
!task.next()
Regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2023 11:42 AM
Here is the code after I added what you recommended, and it did not work. Also I add the ad hoc task within the SCTASK tab. See screen shots. Thanks for your help!
(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();
// If no active tasks are found, close the ritm
if (!task.hasNext()) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
ritm.state = 3; // Closed state
ritm.update();
}
}
})(current, previous);