Background (fix) script to set the RITM stage to Completed when all associated tasks are no longer active

MB12
Kilo Sage

We have order guide requests from the past that did not close properly because the RITM stages were never set to completed in the workflow. The workflows have since been fixed.

 

I'm thinking the script would need to check that all tasks associated with an RITM are inactive, then set the RITM stage to completed. I'm hoping that this can be done without sending out notifications when the request closes. 

If the RITM stage is set to complete, the OOB business rule should trigger and close out the parent request.

 

Can anyone help with the script, or maybe provide a better alternative?

 

Thank you,

1 ACCEPTED SOLUTION

Glad to know that my script helped.

You can additional filter conditions for your query to only select particular catalog item's RITM

updateRecords();

function updateRecords(){

try{

var itemNames = 'Catalog Item1,Catalog Item2'; // give your catalog item names here

var ritm = new GlideRecord("sc_req_item");
ritm.addActiveQuery();
ritm.addEncodedQuery("stage!=complete");
ritm.addEncodedQuery('cat_item.nameIN', itemNames);
ritm.query();
while(ritm.next()){
    //check for active sc tasks
    var taskRec = new GlideRecord('sc_task');
    taskRec.addActiveQuery();
    taskRec.addQuery("request_item",ritm.sys_id);
    taskRec.query();
    if(!taskRec.next()){
        //close requested item
        ritm.stage = "complete";
        ritm.active = false;
        ritm.state = 3; // set to close complete
        ritm.setWorkflow(false); // stop any BR
        ritm.update();
        //close request
        var request = ritm.request.getRefRecord();
        request.active = false;
        request.state = 3;
        request.setWorkflow(false);
        request.update();
    }
}

}

catch(ex){
gs.info('Exception'+ex);
}

}

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Hi Ankur

It's working fine for the stage update with in the form but if I go check in list view stage fields in the blue color.

karan15
Tera Contributor

CAN I PLEASSE GET MORE INSTRUCTIONS WHERE TO ADD THIS SCRIP TO ?