when related incident are closed the problem should close automatically.

Bruce lee
Tera Contributor

->when related incidents are closed the problem should close automatically. Even one incident is open the problem should not close it should show a pop-up message like please close the incident.

->This is the code I'm using but it's not working I think we have to glide the problem table and fetch how many incident records are there 

->How to Glide the problem table and fetch how many incidents records are there?

 

(function executeRule(current, previous /*, g*/ ) {
    var inc = new GlideRecord('incident');
    inc.addQuery('problem_id', current.getValue('problem_id'));
    inc.addActiveQuery();
    inc.query();
    gs.addInfoMessage("Working"+inc.number);
    
    var allIncidentsClosed = true;
    while (inc.next()) {
        if (inc.getValue('state') != '7') { //close
            allIncidentsClosed = false;
            break;
        }
    }
    if (allIncidentsClosed) {
        current.problem_id.state = '107';//close
        current.problem_id.update();
    } else {
        gs.addErrorMessage('There are open incidents related to this problem. The problem cannot be closed until all incidents are closed.');
        current.setAbortAction(true);
    }
})(current, previous);
1 REPLY 1

Weird
Mega Sage

So you just want to close the problem when the last incident is closed?
Yes, you need to do a GlideRecord query on the problem table as you can't dot walk to update it.

Here's an example script for closing the problem:

	var problemID = current.getValue('problem_id');
	var inc = new GlideRecord('incident');
	inc.addQuery('problem_id', problemID);
	inc.addQuery("stateNOT IN6,7,8");
	inc.query();

	if(!inc.hasNext()){
		var problem = new GlideRecord('problem');
		problem.get(problemID);
		problem.state = '107';
		problem.update();
	}

You don't need to do any extra tricks. Simply make sure this script is on a AFTER update BR, so that the current incident is also properly closed. Then, when this BR runs, it checks whether any are active and closes the problem if none are. You could include an alert to notify that problem was also closed.

The condition checks that there are no incidents with state Resolved, Closed or Cancelled. Resolved is an active state, so it would show up with addActiveQuery, so you need to separately check the states instead.

After query you can check inc.hasNext() which will be true if the query returns any records. By adding a "!" before it we are reversing the result. Basically true becomes false and vise versa. In this case if no records are found we'll get true in the if condition and the script will then update the problem state.