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

Vijay8
Giga Contributor

I want to close all child incidents before closing parent incident, if any one child incident not closed the parent incident should not be closed should populate a error message

I tried the code below but, If I try to close one child Incident parent incident is also getting closed 

I tried many solutions in community but, none of them work please help me with this

 

 

var inc = new GlideRecord('incident');
inc.addQuery('parent_incident', current.sys_id);
inc.addQuery('sys_id', '!=', current.sys_id);
inc.addActiveQuery();
inc.query();
if (!inc.next()) {

var parInc = new GlideRecord('incident');
parInc.addQuery('sys_id', current.parent_incident);
parInc.query();
if (parInc.next()) {
parInc.state = 6;
parInc.active = false;
parInc.update();
}
}

1 ACCEPTED SOLUTION

Hi Vijay,

Actually in resolved state the active flag is true only and in closed state it is setting to false.

Try updating the filter condition as shown below:

find_real_file.png

 

Remove the resolved part and try putting infomessage inside the code and see which info message is coming in the below sctipt:

//Check if all hte task are closed or not
var inc = new GlideRecord('incident');
inc.addQuery('parent_incident', current.getValue('parent_incident'));
inc.addActiveQuery();
inc.query();
if(!inc.hasNext()) {

gs.addInfoMessage('@inside Loop ' + current.getValue('parent_incident'));
//If task are closed then closed the parent ticket.
var parInc = new GlideRecord('incident');
parInc.get(current.getValue('parent_incident'));
gs.addInfoMessage('parent incident nubmer ' + parInc.getValue('number'));

parInc.state = 6 //State value which you want to set
parInc.update();

}

gs.addInfoMessage('Inside BR');

 

 

 

 

Thanks,

CB

View solution in original post

27 REPLIES 27

Ram102
Kilo Contributor

Not checked much..but one thing got immediate attention is "if" ..you may need while here..

try .. 

Tejas Tamboli
Giga Guru

Hello Vijay,

Did you try this if no then try below code:

 

Create an Before update Business rule on incident table and add the condition as State changes to Resolved.

 

var gr = new GlideRecord('incident');
gr.addQuery('parent', current.sys_id);

gr.addQuery('state', 'IN', '1,2,3'); // Replace your incident state values like Work in progress, New, Completed, etc., except resolved or closed

gr.query();

if(gr.next()) {

gs.addErrorMessage("There is an active child incident, please resolve the child incident, before closing parent');

current.setAbortAction(true);

}

 

For more details you can refer below link:

https://community.servicenow.com/community?id=community_question&sys_id=d1ffffa8dbc2bb4014d6fb243996...

https://community.servicenow.com/community?id=community_question&sys_id=2ec1c240db11a740e0e80b55ca96... 

 

 

I hope this will help you.

 

Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response useful to you and help others to find information faster.

Thanks,
Tejas

Vijay8
Giga Contributor

Hi Teja,

Thank you for Immediate reply but, I already tried this it's not working 

 

Did you add conditions as States changes to Closed or States changes to Resolved. 

 

Thanks and Regards,

Tejas