Prevent Incident resolution if Open Incident Tasks

sndev1099
Giga Expert

Hi,

I'm looking for some help with my before Business Rule to prevent an Incident being resolved if there are open Incident Tasks. I've done the following so far:

When to run: Before
Order: 100
Update: Selected
Filter Conditions: 'State' 'changes to' 'Resolved'

This is my script:

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

 var incgr = new GlideRecord('incident_task');
 incgr.addActiveQuery();
 incgr.addQuery('incident',current.sys_id);
 incgr.query();
 if (incgr.hasNext()) {
 
  current.setAbortAction(true);
  gs.addErrorMessage('All Incident Tasks must be closed before an Incident can be resolved');
  action.setRedirectURL(current.sys_id);
 
  }

})(current, previous);

This is finding the open incident tasks and providing the error message. The problem is, it still moves the status to 'Resolved'. Only it hasn't committed this to the DB as if you click 'Reload Form' it moves the state back to what it was previously. So technically, the BR is working but I think it's getting stuck somewhere. I've tried adding current.state = previous.state; to it but that doesn't do anything either.

Am I doing something wrong here? New to ServiceNow so any help would be great. Thanks.

12 REPLIES 12

Michael Fry1
Kilo Patron

State - changes to - Resolved is good for the condition

But your script should count the number of active incident tasks. If more than 1, then prevent Resolution and show error message. Script looks like this:

//Query incident_task table to see if there are any active tasks
    var incidentTask = new GlideRecord('incident_task');
    incidentTask.addQuery('parent', current.sys_id);
    incidentTask.addActiveQuery();
    incidentTask.query();
    
    var count = incidentTask.getRowCount();
    
    if(count == 1){
        gs.addInfoMessage("You have "+count+" open incident task which must be completed before this record can be resolved");
        current.setAbortAction(true);
    }
    if(count > 1){
        gs.addInfoMessage("You have "+count+" open incident tasks which must be completed before this record can be resolved");
        current.setAbortAction(true);
    }
    else{
        //gs.addInfoMessage("All tasks have been completed");
    }

Thanks for the reply Michael. Unfortunately, this does the same thing. It finds the open tasks but still sets the State to Resolved. You then have to right click and select 'Reload Form' for the state to move back to its original state. In this example, it moves back to 'In Progress'.

Well you could use current.state = previous.state; like you tried but need to add an update to the script. Do you really want to write everything else, but reset the State?

Still no luck. Do you think I need to amend the UI Action for the Resolve button and add some script in there?