Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

I'm trying this way if all child incidents are closed then parent incident should auto closed

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

    if (current.state.changesTo('7')) {
        var task = new GlideRecord('incident_task');
        task.addQuery('incident', current.parent_incident);
        task.addQuery('state', '!=', '7');
        task.query();

        if (!task.hasNext()) {
            var inc = new GlideRecord('incident');
            if (inc.get(current.parent_incident)) {
                inc.state = '7';
                inc.description = "incident tasks are cosed hence closing the incident";
                inc.setWorkflow(false);
                inc.update();
            }
        }
    }

})(current, previous);



Even after closing the child incident parent incident is not updating.
1 REPLY 1

M Iftikhar
Tera Sage

Hello @ypranavi4,

 

It looks like the reason your parent incident isn’t updating is because when an Incident is closed in ServiceNow, the platform enforces mandatory fields like Resolution code and Resolution notes. In your current script you’re only setting state and description, so the update fails silently since required fields aren’t being set.

You can fix this by also providing values for those fields before the update, for example:

if (current.state.changesTo('3')) {
    var task = new GlideRecord('incident_task');
    task.addQuery('incident', current.incident); //for incident task parent, the field is incident
    task.addQuery('state', '!=', '3');
    task.query();

    if (!task.hasNext()) {
        var inc = new GlideRecord('incident');
        if (inc.get(current.incident)) {
            inc.state = '7'; // Closed
            inc.close_notes = "Incident tasks are closed, hence closing the incident";
            inc.close_code = "No resolution provided"; // required field
            inc.setWorkflow(false);
            inc.update();
        }
    }
}

This way, the parent incident can transition to the Closed state successfully, since both resolution code and notes are being populated.

Thanks & Regards,  

Muhammad Iftikhar    

If my response helped, please mark it as the accepted solution so others can benefit as well. 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.