I want to close all child incidents after closing parent incident?

JMR2
Mega Expert

Hi all,

I want to close all child incidents after  closing parent incident, if any one child incident not closed the parent incident should not be close? 

updateChildIncidents();
function updateChildIncidents() {
var msg = "";
var commentsMsg="";
var grIncident = new GlideRecord("incident");
grIncident.addActiveQuery();
grIncident.addQuery("parent_incident", current.sys_id);
grIncident.query();
while(grIncident.next()){
if(current.state.changesTo("6")){
gs.addInfoMessage('incident state'+grIncident.state);
if(grIncident.state=="8") // If the child incident in state awaiting for parent, we resolve it.
grIncident.state = "6";
commentsMsg = "Resolved based on resolution of Parent Incident.";
}
if(current.state.changesFrom("6") && !current.state.changesTo("7")){ // If the parent incident is reopened we update the state of child if was resolved by the parent
if(grIncident.state=="6" && grIncident.close_notes.toString().indexOf("Solution copied from Parent Incident")!=-1) {
grIncident.state = "8";

if(!current.work_notes){
grIncident.work_notes = "Parent Incident reopened";
}
}
}
if(current.close_notes.changes()){
msg = "Solution copied from Parent Incident";
if (current.close_notes.toString().indexOf(msg) == 0)
grIncident.close_notes = current.close_notes;
else
grIncident.close_notes = msg + ": " + current.close_notes;
}
if(current.close_code.changes()){
grIncident.close_code = current.close_code;
}
copyCommentsAndWorkNotesFromParent(grIncident,commentsMsg,null);
grIncident.update();
}
}
function copyCommentsAndWorkNotesFromParent(incident,commentsMsg, notesMsg){
if(current.comments) {
if(!commentsMsg)
commentsMsg = "Comment copied from Parent Incident";
if (current.comments.toString().indexOf(commentsMsg) == 0)
incident.comments = current.comments;
else
incident.comments = commentsMsg + ": " + current.comments;
}
if(current.work_notes) {
if(!notesMsg)
notesMsg = "Work note copied from Parent Incident";
if (current.work_notes.toString().indexOf(notesMsg) == 0)
incident.work_notes = current.work_notes;
else
incident.work_notes = notesMsg + ": " + current.work_notes;
}
}

7 REPLIES 7

AbhishekGardade
Giga Sage

hello JMR,

1. Create a new business rule as below:

find_real_file.png

2. Add below code in above BR;

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

// Add your code here
var closedChild = 0;
var grInc = new GlideRecord('incident');
grInc.addQuery('parent_incident',current.sys_id);
grInc.query();
var totalChildCount = grInc.getRowCount();
gs.addInfoMessage("CNT: "+totalChildCount);

while(grInc.next()){
if(grInc.state == 6){
closedChild +=1;
}
}
if(closedChild != totalChildCount){
gs.addErrorMessage("please close All child incident");
current.setAbortAction(true);
}

})(current, previous);

 

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Hi  Abhishek Gardade.

thanks for your reply.

but my requirement is little bit different, if parent is closed and child should be closed.  

 

Hello JMR,

find_real_file.png

Modify above business rule as below:

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

// Add your code here
var closedChild = 0;
var grInc = new GlideRecord('incident');
grInc.addQuery('parent_incident',current.sys_id);
grInc.query();
while(grInc.next()){

grInc.incident_state = current.incident_state;
grInc.update();

}


})(current, previous);

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

thanks for your reply.

still Not working Abhishek.