BR to close REQ when All related RITMs closed

Karishma Dubey
Tera Expert

Hi,

I am closing RITM when its related sc_task is completed from below BR which is on sc_task table. I want to close REQ once all related RITM is closed(there will be multiple RITM). Does before BR will help to achieve this? OOB BR is not working in my case. 

 

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

// Add your code here
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', current.request_item);
gr.query();
if (gr.next()) {
gr.state = '3';
gr.stage = 'Completed';
gr.update();

}
})(current, previous);

KarishmaDubey_0-1707757597537.png

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Karishma Dubey Please update your business rule script as follows.

 

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

    var gr = new GlideAggregate('sc_req_item');
    gr.addQuery('request', current.request);
    gr.addAggregate('COUNT');
    gr.query();
    var count=0;
    if (gr.next()) {
    count=gr.getAggregate('COUNT'); //Get all task count
    }

    var glideRITM = new GlideAggregate('sc_req_item');
    glideRITM.addQuery('request',current.request);
    glideRITM.addQuery('state','3'); //Get Closed complete task count
    glideRITM.addAggregate('COUNT');
    glideRITM.query();
    var closedTaskCount=0;
    if (glideRITM.next()) {

    closedTaskCount=glideRITM.getAggregate('COUNT');
    if(count==closedTaskCount){
     var glideRequest=current.request.getRefRecord();
    glideRequest.setValue('state','closed_complete'); //close the request if count of all task and closed task same.
    glideRequest.update();
    }
    }

})(current, previous);

Hope this helps.

View solution in original post

9 REPLIES 9

Why are you creating another RITM from the Flow? Are you requesting another item as part of your flow?

Abhijit
Tera Expert

This is not going to work. 

 

var gr = new GlideRecord('sc_req_item');        ---------- it should be sc_request for REQ records
gr.addQuery('sys_id', current.request_item);  ---------   sys_id is fine but you need to pass value of REQ sys_id which "request" column
gr.query();
if (gr.next()) {     /// this will only close 1 record, you mentioned REQ will have many RITM records associated, replace with while 
gr.state = '3';
gr.stage = 'Completed';   /// check for the fields which are mandatory for closer of REQ, based on REQ process for your environment pass fields such closure code and comments
gr.update();

}

Hi 

I am writing this BR to close the ritm first when sc task completes and once all the ritm close I need to close the req

Then logic is not correct, how you are deciding this request is last one

You need to query sc_req_item and check with all records where request is REQ and all other RITM other than current are closed

Sandeep Rajput
Tera Patron
Tera Patron

@Karishma Dubey Please update your business rule script as follows.

 

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

    var gr = new GlideAggregate('sc_req_item');
    gr.addQuery('request', current.request);
    gr.addAggregate('COUNT');
    gr.query();
    var count=0;
    if (gr.next()) {
    count=gr.getAggregate('COUNT'); //Get all task count
    }

    var glideRITM = new GlideAggregate('sc_req_item');
    glideRITM.addQuery('request',current.request);
    glideRITM.addQuery('state','3'); //Get Closed complete task count
    glideRITM.addAggregate('COUNT');
    glideRITM.query();
    var closedTaskCount=0;
    if (glideRITM.next()) {

    closedTaskCount=glideRITM.getAggregate('COUNT');
    if(count==closedTaskCount){
     var glideRequest=current.request.getRefRecord();
    glideRequest.setValue('state','closed_complete'); //close the request if count of all task and closed task same.
    glideRequest.update();
    }
    }

})(current, previous);

Hope this helps.