Related incidents still not closing after activating "SNC - ITIL - Close Related"

Abbottronix
Tera Guru

I want related incidents to auto-close when I close the problem they're related to. There's an OOTB business rule on the problem table that appears to do this: SNC - ITIL - Close Related 

 

However, after activating it, my related incidents still aren't closing. Does anyone know what the issue might be?

 

Here is the script of the OOTB rule:

 

if (current.problem_state.changesTo(4)) {
   closeRelatedIncidents(current);
   closeRelatedTasks(current);
}

// 
// Close any incidents that are related to the current problem
//
function closeRelatedIncidents(me) {
  var incident = new GlideRecord("incident");
  incident.addQuery("problem_id", "=", me.sys_id);
  incident.query();
  while (incident.next()) {
    if ( incident.active == true ){
      var msg = gs.getMessage("Incident {0} closed based on closure of problem {1}", [incident.number, me.number]);
      gs.print(msg);
      incident.incident_state.setValue(IncidentState.CLOSED);
      incident.active.setValue(false);
      incident.comments = msg;
      incident.update(); 
    }
  }
}
2 REPLIES 2

Maddysunil
Kilo Sage

@Abbottronix 

1.  Ensure that the problem state is indeed changing to the desired state (in this case, state 4). If the problem state is not changing as expected, the business rule won't trigger.

2. You can add additional logging statements to the script to see if it's being triggered and if any errors are occurring. This can help pinpoint where the issue lies.

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

Krecker
Tera Expert

I stumbled upon the same issue. The Script seems to be a bit outdated.

1. Check the choice list of the field 'problem_state'. On my dev instance the standard value for 'closed' was 107 instead of 4.
Change the first line of the script accordingly, so it gets triggered correctly. Safest is your use the constant 

'ProblemState.CLOSED'
if (current.problem_state.changesTo(ProblemState.CLOSED)) {


2. To close an incident, close notes and resolution code are mandatory and enforced by a Data Policy. As these are not provided by the script, its blocking the update.
Add these lines to the script.

incident.close_notes = msg;
incident.close_code = 'Resolved by problem';

 

All in all my now working script looks like this:

if (current.problem_state.changesTo(ProblemState.CLOSED)) {
    closeRelatedIncidents(current);
    closeRelatedTasks(current);
}

// 
// Close any incidents that are related to the current problem
//
function closeRelatedIncidents(me) {
    var incident = new GlideRecord("incident");
    incident.addQuery("problem_id", "=", me.sys_id);
    incident.query();
    while (incident.next()) {
        if (incident.active == true) {
            var msg = gs.getMessage("Incident {0} closed based on closure of problem {1}", [incident.number, me.number]);
            gs.print(msg);
            incident.incident_state.setValue(IncidentState.CLOSED);
            incident.active.setValue(false);
            incident.comments = msg;
            incident.close_notes = msg;
			incident.close_code = 'Resolved by problem';
            incident.update();
        }
    }
}


Hope it helps.