Need help with a BR that will close the RITM after the last SCTASK is closed

Julie Catano
Kilo Guru

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);

2 ACCEPTED SOLUTIONS

Eswar Chappa
Mega Sage
Mega Sage

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 

View solution in original post

Julie Catano
Kilo Guru

I added the latest code and then also had to run this BR too.  See below

(function executeRule(current, previous /*null when async*/) {
 
// Add your code here
var SC_task = new GlideRecord('sc_task');
SC_task.addQuery('active', true);
SC_task.addQuery('request_item', current.sys_id);
SC_task.query();
if(SC_task.next()){
 
gs.addInfoMessage('Your task is not closed please close them first');
current.state = previous.state;
current.setAbortAction('true');
 
}
})(current, previous);

 

View solution in original post

10 REPLIES 10

try  below one , slightly modified

 

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() && task.getRowCount == 1) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
ritm.state = 3; // Closed state
ritm.update();
}
}

Julie Catano
Kilo Guru

I copied the above code into my BR and the RITM is still closing complete even when my ad hoc task is still open.  I can see it's running the BR but it appears it's not recognizing the ad hoc task that is still open.

Abhijit
Tera Expert

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();
}
}

Eswar Chappa
Mega Sage
Mega Sage

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 

I tried both of the above BR code and the ad hoc task was open and the RITM still closed complete.  I have attached the workflow that we are using.  Is there something in the workflow that needs to be changed?