Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Close SCTASK and RITM when Request is Closed

mallikabhupathi
Tera Expert

I need to close the RITM and the SCTASK associated with the Request when the Request is Closed.

 

How can I achieve this?

 

Thanks,

Mallika

3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

Hi @mallikabhupathi ,

 

You can create a business rule on sc_request table like the one below. (This might not be a best practice to close from REQ -> RITM -> Task, but if this is a requirement you can use the following.

 

When to Run: after Insert / Update

Condition: State :: Changes to :: Closed

 

Script:

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

    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('request', current.sys_id);
    ritm.query();
    while (ritm.next()) {
        var sct = new GlideRecord('sc_task');
        sct.addQuery('request_item', ritm.sys_id);
        sct.query();
        while (sct.next()) {
            sct.state = '3';
            sct.update();
        }
        ritm.state = '3';
        ritm.update();
    }
})(current, previous);

 

AnveshKumarM_0-1695353984000.png

 

AnveshKumarM_1-1695354009264.png

 

 

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

Thanks,
Anvesh

Ankur Bawiskar
Tera Patron
Tera Patron

@mallikabhupathi 

Ideally SC task gets closed then RITM and REQ.

Why it's the reverse way for your case?

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

Eswar Chappa
Mega Sage

Hi @mallikabhupathi you can achieve it flow designer with out code with conditions below:

 

EswarChappa_0-1695358267792.png

EswarChappa_1-1695358317633.png

EswarChappa_2-1695358345867.png

 

Thanks & Regards,

Eswar Chappa

Mark my answer correct and Helpful if this helps you 😀