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

Karthiga S
Kilo Sage

HI @Julie Catano 

 

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

Julie Catano
Kilo Guru

Unfortunately, the ad hoc task (not in workflow) is still open and the RITM closed.  See screen shots.

Dan Covic2
Tera Contributor

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

Julie Catano
Kilo Guru

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