check if all child incidents attached to parent incident has state value equals to closed or not

akshay parekar1
Tera Contributor

Hello,

i have written a before business on update for incident table with condition "incident state is resolved"

first i want to get the count of child incidents with state not equals to closed

and after that, if all child incidents are closed, state of parent incident should be resolved ,otherwise show error message that close the child incidents

business rule:

(function executeRule(current, previous /*null when async*/) {
    
    var closedchild=0;
var gr= new GlideRecord('incident');
    gr.addQuery('parent_incident',current.sys_id);
    gr.query();
    
    var countofchild= gr.getRowCount();
    gs.addInfoMessage("child incidents which are not closed are" +countofchild );
    
    while(gr.newRecord()){
        if(gr.state==6){
            closedchild +=1;
        }
    }
    if(closedchild!=countofchild){
        gs.addErrorMessage("Please close all the child incidents");
        current.setAbortAction(true);
    }
})(current, previous);

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Hi Akshay,

Update your script as below

 var closedchild=0;
var gr= new GlideRecord('incident');
    gr.addQuery('parent_incident',current.sys_id);
    gr.addQuery('state', '!=', '7');  //check backend value of closed
    gr.query();
    var countofchild= gr.getRowCount();
    gs.addInfoMessage("child incidents which are not closed are" +countofchild );
    if(gr.next())
    {
        gs.addErrorMessage("Please close all the child incidents");
        current.setAbortAction(true);
    }
    

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

View solution in original post

4 REPLIES 4

SumanthDosapati
Mega Sage
Mega Sage

Hi Akshay,

Update your script as below

 var closedchild=0;
var gr= new GlideRecord('incident');
    gr.addQuery('parent_incident',current.sys_id);
    gr.addQuery('state', '!=', '7');  //check backend value of closed
    gr.query();
    var countofchild= gr.getRowCount();
    gs.addInfoMessage("child incidents which are not closed are" +countofchild );
    if(gr.next())
    {
        gs.addErrorMessage("Please close all the child incidents");
        current.setAbortAction(true);
    }
    

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

as how?

I updated now.

Mohith Devatte
Tera Sage
Tera Sage

hello @Akshay Parekar ,

try this script 

(function executeRule(current, previous /*null when async*/) {
    
  var closedchild=0;
var gr= new GlideRecord('incident');
 gr.addQuery('parent_incident',current.sys_id);
gr.addQuery('state','!=','close choice value') //replace with closed choice value
    gr.query();
    
    var countofchild= gr.getRowCount();

 if(countofchild>0)
{
    gs.addInfoMessage("child incidents which are not closed are" +countofchild );
}
else
{
current.state="resolved choice value"
    
}
})(current, previous);

PLEASE MARK MY ANSWER CORRECT IF IT HELPS YOU