Cancel a Change Request on Insertion If Requester Has Many open Changes

issarodinho
Kilo Expert

Hello,

I have a business requirement to deny and Cancel any new change request of a given requester if they have 5 or more open CRs until they close/work on them. This is to ensure they close their CRs.

 

I am using the below script but it is not working as per my requirement. it is proceeding to create a new CR without marking it as cancelled.

Anyone to guide me where i am not doing it right? Below is the script in Business logic implemented on insertion.

 

(function executeRule(current, previous /*null when async*/) {
var queryString = "active=true^stateIN-1,0";
var count =new GlideAggregate('change_request');
var record =new GlideRecord('change_request');
record.addQuery('State','New');
record.addQuery('State',current.State);
count.addQuery('active','true');
count.addQuery('requested_by',current.requested_by);
count.addEncodedQuery(queryString);
count.addAggregate('COUNT');
count.query();
var change =0;
if(count.next())
change = count.getAggregate('COUNT');
if(change>=5){
gs.addErrorMessage('You have multiple Change Requests pending for closure, please close them before issuing a new CR"');
record.setValue('State','Cancelled');
record.close_code = "Unable to successfully raise the CR due to multiple open records.";
//current.setAbortAction(true);
}
})(current, previous);

0 REPLIES 0