RITM should be in open state when any manual task is open

Kasia5
Tera Contributor

Hi All

 

I have a task that RITM should stay in Open state until all the tasks will be closed.

Tasks are mainly created by workflow but there is also possibility to create task manually from RITM.

Workflow automatically close the RITM when all of the workflow tasks are closed.

So I've created a Before Update Business Rule on sc_req_item table:

Condition: current.state.changesTo('5')       // 5 - Fulfillment Completed

Script:

(function executeRule(current, previous) {

    gs.info("RITM validation BR triggered for " + current.number);

    var scTask = new GlideRecord('sc_task');
    scTask.addQuery('request_item', current.sys_id);
    scTask.addActiveQuery();
    scTask.addNullQuery('wf_activity');
    scTask.query();

    if (scTask.hasNext()) {

        gs.addErrorMessage(
            'Request Item cannot be Closed while manual tasks are still open.'
        );
        current.state = previous.state;
        current.setAbortAction(true);
    }

})(current, previous);
So thanks to this BR RITM is still in Open state when all of the workflow tasks are closed but there is still manual task open.
 
To close the RITM I've created the second BR on sc_task table:
After Update
Condition: When state changes to Closed Complete or Closed Skipped or Closed Incomplete
Script:
(function executeRule(current, previous) {

    var ritmId = current.request_item;

    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', ritmId);
    gr.addQuery('state', 'NOT IN', '3,4,7');
    gr.setLimit(1);
    gr.query();

    if (!gr.hasNext()) {

        var ritm = new GlideRecord('sc_req_item');

        if (ritm.get(ritmId)) {

            if (ritm.state != 5) {

                ritm.state = 5;
                ritm.stage = "Fulfillment Complete";
                ritm.u_fulfilled_at = gs.nowDateTime();
                ritm.active = false;

                ritm.update();
            }
        }
    }

})(current, previous);
 
First BR is working fine but second one change the state when:
First workflow task is created, I've added the manual task, I've closed the forst workflow task, the secodn worfkflow task is created, I've closed the manual task and then close the second workflow task. RITM  state has been changed to closed complete but in meantime third workflow task has been created...
 
Any advice how to create the second BR so it will work correct?
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@Kasia5 

don't close RITM from Workflow

My thoughts

-> do not close the RITM while its workflow context is still executing

Update your sc_task After Update BR as this

(function executeRule(current, previous) {

    if (current.request_item.nil()) {
        return;
    }

    var ritmId = current.request_item.toString();

    // 1. Are there any active catalog tasks still open?
    var openTask = new GlideRecord('sc_task');
    openTask.addQuery('request_item', ritmId);
    openTask.addActiveQuery();
    openTask.setLimit(1);
    openTask.query();

    if (openTask.hasNext()) {
        return;
    }

    // 2. Is workflow still running for this RITM?
    var wf = new GlideRecord('wf_context');
    wf.addQuery('id', ritmId);
    wf.addQuery('table', 'sc_req_item');
    wf.addQuery('state', 'IN', 'executing,waiting');
    wf.setLimit(1);
    wf.query();

    if (wf.hasNext()) {
        return;
    }

    // 3. Safe to close RITM
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get(ritmId)) {
        if (ritm.state != 5) {
            ritm.state = 5;
            ritm.stage = 'fulfillment_complete';
            ritm.u_fulfilled_at = gs.nowDateTime();
            ritm.active = false;
            ritm.update();
        }
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Kasia5 

don't close RITM from Workflow

My thoughts

-> do not close the RITM while its workflow context is still executing

Update your sc_task After Update BR as this

(function executeRule(current, previous) {

    if (current.request_item.nil()) {
        return;
    }

    var ritmId = current.request_item.toString();

    // 1. Are there any active catalog tasks still open?
    var openTask = new GlideRecord('sc_task');
    openTask.addQuery('request_item', ritmId);
    openTask.addActiveQuery();
    openTask.setLimit(1);
    openTask.query();

    if (openTask.hasNext()) {
        return;
    }

    // 2. Is workflow still running for this RITM?
    var wf = new GlideRecord('wf_context');
    wf.addQuery('id', ritmId);
    wf.addQuery('table', 'sc_req_item');
    wf.addQuery('state', 'IN', 'executing,waiting');
    wf.setLimit(1);
    wf.query();

    if (wf.hasNext()) {
        return;
    }

    // 3. Safe to close RITM
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get(ritmId)) {
        if (ritm.state != 5) {
            ritm.state = 5;
            ritm.stage = 'fulfillment_complete';
            ritm.u_fulfilled_at = gs.nowDateTime();
            ritm.active = false;
            ritm.update();
        }
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Works perfect, thank you!

TharaS657398130
Giga Guru

Hi,

I think this issue is happening because your second Business Rule checks only for currently open tasks at the moment a task is closed, but the workflow may still create new workflow tasks immediately after that. So when the manual task is closed, the BR thinks all tasks are completed and closes the RITM before the next workflow task gets generated. Instead of directly closing the RITM from the sc_task BR, you should also validate that the workflow itself is completed (or add a small delayed check using event/async logic).

A better approach would be to query all active sc_task records for the RITM after workflow execution is fully finished, rather than checking immediately on task closure. This avoids the race condition where new workflow tasks are created after your validation runs.