Business Rule to Set REQ State to closed Complete or Closed Incomplete as a Roll up from SC Task

oluseyiasol
Tera Contributor

I need help in getting a business rule to work.  As follows   

It fires off the stask and rolls up to the RITM and REQ, and it also populates the duration and closed date/time fields fields.  So far i have gotten it to the point of closing the RITMS but not the duration/date fields.

1 REPLY 1

Tanushree Maiti
Tera Patron

Hi @oluseyiasol 

 

You can try with an after update BR on sc_task:

 

Condition: State changes to Closed Complete OR Closed Incomplete

 

Sample code/Not tested

 

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

    var ritmSysId = current.request_item;

    var scTaskGr = new GlideRecord('sc_task');

    scTaskGr.addQuery('request_item', ritmSysId);

    scTaskGr.addActiveQuery();

    scTaskGr.query();

    if (!scTaskGr.hasNext()) {

         // Calculate Duration from Task to RITM

        var openedAt = new GlideDateTime(current.opened_at);

        var closedAt = new GlideDateTime(current.closed_at);

        var duration = GlideDateTime.subtract(openedAt, closedAt);

 

        var ritmGr = new GlideRecord('sc_req_item');

        if (ritmGr.get(ritmSysId)) {

            ritmGr.state = current.state;

            ritmGr.setValue('closed_at', current.closed_at);

            ritmGr.setValue('business_duration', duration);

            ritmGr.update();

        }

        var reqGr = new GlideRecord('sc_request');

        if (reqGr.get(current.request.sys_id)) {

            reqGr.state = current.state;

            reqGr.setValue('closed_at', current.closed_at);

            reqGr.update();

        }

    }

})(current, previous);

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti