Close RITM when ALL related Change requests have been closed

FredrikT
Tera Guru

I have a Catalog Item using traditional workflow with multiple SCTASKs that eventually results in one or more related Change Requests being attached to the RITM.  I am trying to have the RITM close ONLY when ALL related Change Requests have been closed.  Currently using a Wait for condition in the workflow for "Wait for Change to Close" the RITM gets closed when the 1st related Change Requests is closed, not all of them.  Is there a better way?  Many suggest an after business rule however I have not found any examples that spell out what I need to do for my particular scenario.  

 

Thank you 

1 ACCEPTED SOLUTION

@FredrikT You can use the following script to add that check. You need to replace YOUR_CATALOG_ITEM_SYS_ID in the script with your catalog item sys_id.

 

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord("change_request");
    gr.addQuery("parent", current.parent.sys_id);
    gr.addEncodedQuery("stateNOT IN3,4");
    gr.query();
    if (!gr.hasNext()) {
        var ritmGr = new GlideRecord("sc_req_item");
        if (ritmGr.get(current.parent.sys_id)) {
            var cat_item_id = ritmGr.getValue('cat_item') + '';
            if (cat_item_id == "YOUR_CATALOG_ITEM_SYS_ID") {
                ritmGr.state = '3';
                ritmGr.stage = 'closed_complete';
                ritmGr.update();
            }
        }
    }

})(current, previous);

 

Please mark my answer helpful and accept as solution if it helped you 👍✔️

 

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

@FredrikT It is not  good practice to listen for the state of change_request in a work flow which handles the RITM. Because RITM workflow will not listen for record changes of change_request table. It is better to handle them in business rule only.

Are you creating 

If you want the logic for BR I can help you.

Thanks,
Anvesh

Great.  yes,  I could use help with the BR logic to set the RITM to closed complete when all related Changes Requests are closed for a particular item.

@FredrikT try this code in your After Update Business Rule on Change Request table.

 

(function executeRule(current, previous){

var gr = new GlideRecord("change_request");

gr.addQuery("parent", current.paren.sys_id);

gr.addEncodedQuery("stateNOT IN3,4");

gr.query();

if(!gr.hasNext()){

var ritmGr = new GlideRecord("sc_req_item");

if(ritmGr.get(current.parent.sys_id)){

ritmGr.state = '3';

ritmGr.stage = 'closed_complete';

ritmGr.update();

}

}

})(current, previous)

 

Please mark my answer helpful and accept as solution if it helped you 👍

Thanks,
Anvesh

FredrikT
Tera Guru

Thank you, that is very helpful but this condition is only for a particular RITM not any/all RITMs with related Change Requests so how would I call that out to only apply if cat_item or the RITM = [a specific item]  ?