Abort RITM closure if work order or work order task is active

Ap_1
Tera Contributor

Hi,

I have a requirement that if user directly closing the RITM, system will check if there is any active work order or active work order task for that work order is there or not.

If yes, then system will abort closing the RITM.

 

I have created a before business rule with condition, state changes to closed complete and used below script.

 

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

var rec = current.sys_id;
var gr = new GlideRecord('wm_order');
gr.addQuery('initiated_from', rec);
gr.addQuery('active', true);
gr.query();

var ga = new GlideRecord('wm_task');
ga.addQuery('initiated_from.parent', rec);
ga.addQuery('active', true);
ga.query();

if(gr.hasNext() || ga.hasNext()){
    current.setAbortAction(true);
    gs.addErrorMessage('Please close the related active work order or work order task to close the RITM');
}

})(current, previous);
 
Lets say, a RITM has one work order and the work order has 4 work order task. Now if I close the work order task one by one till the 3rd task, RITM is aborting properly. But when the last work order task is open, if I am trying to close the RITM, it is directly closing.
Checked by adding log also, for the last one, it is not going inside the hasnext() condition, though for the first two glide, it has record. 
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

I would keep the two GlideRecords separate, as there is nothing gained by combining them:

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

    var rec = current.sys_id;
    var gr = new GlideRecord('wm_order');
    gr.addQuery('initiated_from', rec);
    gr.addQuery('active', true);
    gr.query();
	if (gr.next()){
		current.setAbortAction(true);
        gs.addErrorMessage('Please close the related active work order to close the RITM');
	}

    var ga = new GlideRecord('wm_task');
    ga.addQuery('initiated_from.parent', rec);
    ga.addQuery('active', true);
    ga.query();
    if (ga.next()) {
        current.setAbortAction(true);
        gs.addErrorMessage('Please close the related active work order task to close the RITM');
    }
})(current, previous);

Ap_1
Tera Contributor

Hi Brad,

 

Thanks for your update. I can do that but the requirement is to show only one error message. In the above script system will show two error message every time.

So for that I want to merge both work order and work order task. Can you help me if I did anything wrong on my script.

Since you can't/won't ever have an active work order task without an active work order, this makes more logical sense:

(function executeRule(current, previous /*null when async*/ ) {
    var rec = current.sys_id;
    var gr = new GlideRecord('wm_order');
    gr.addQuery('initiated_from', rec);
    gr.addQuery('active', true);
    gr.query();
	if (gr.next()) {
		var ga = new GlideRecord('wm_task');
		ga.addQuery('initiated_from.parent', rec);
		ga.addQuery('active', true);
		ga.query();
		if (ga.next()) {
			current.setAbortAction(true);
			gs.addErrorMessage('Please close the related active work order task to close the RITM');
		}
	} else {
		current.setAbortAction(true);
        gs.addErrorMessage('Please close the related active work order to close the RITM');
	}
})(current, previous);