RITM needs to be close compley when the sc task are close complete

Akshay Jadhav1
Tera Contributor

Hello All,

 I am working on creating a business rule for the sctask table. The purpose is to close complete the RITM  when all SCTasks are marked as "Closed Complete,".

I have written the following script, which works fine when all tasks are generated concurrently. However, it fails when tasks are generated sequentially, one after another. This happens because using "after update" or "before update" doesn't effectively handle tasks created at a later time.

 

var grScTask = new GlideRecord("sc_task");

    grScTask.addQuery('request_item', current.request_item);

    grScTask.addEncodedQuery("number!=" + current.number + '^state!=3');

    grScTask.query();

    var count = grScTask.getRowCount();

    if (count == 0) {

        var ritm = current.request_item.getRefRecord();

        ritm.state = 3; // closed complete.

        ritm.stage = 'complete';

        ritm.update();

    }


Does anyone have any idea 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Akshay Jadhav1 

BR should be after update

use this

var taskRec = new GlideRecord("sc_task");
taskRec.addEncodedQuery("request_item=" + current.request_item + "^active=true"); // if no active tasks for this RITM exists
if(!taskRec.hasNext()){
    var ritm = new GlideRecord("sc_req_item");
    if(ritm.get(current.request_item)){
        ritm.state = 3;
        ritm.update();
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hello Ankur,
Thank you for your response,

I tried your Business Rule, but it is not working in the situation where the second task is generated after the first one is marked as 'Closed Complete.' I think the second task is being generated after the Business Rule executes.