How to Automatically Close Child Incidents When Parent Incident is Closed via UI Action

vaishali231
Tera Contributor

Hi Community,

I have a requirement where, when a parent Incident is closed using a UI Action, the system should automatically:

Close all associated child Incidents (those with the parent value set).

Update each child Incident with a work note, similar to the one entered in the parent.

Could someone guide me on how to approach this?
Should this logic be added in the UI Action script of the parent? And how can I best loop through and update all child records with the parent's work note?

Any sample scripts or best practices would be appreciated.
Thanks in advance

14 REPLIES 14

You can do this as two parts,
1.Ui Action to close the Parent Incident.
2.Async BR which triggers on closure of incident and checks if incident has no parent incident populated ,

Then queries for child incidents , Closes and updates the worknotes of Child incidents.

Sample BR script:

// Get the parent's latest work note
var parentWorkNote = current.work_notes.getJournalEntry(1); // Gets the most recent work note

// Query and update all child incidents
var childGR = new GlideRecord('incident');
childGR.addQuery('parent', current.sys_id);
childGR.query();
while (childGR.next()) {
childGR.state = 7; // Closed
childGR.work_notes = 'Closed by parent incident. ' + (parentWorkNote ? parentWorkNote : '');
childGR.update();
}

If my response solves your query, please marked helpful by selecting Accept as Solution and Helpful..

Mohammad Danis1
Giga Guru

@vaishali231 , You can update the UI Action if that is not used by anyone else.
But as Kamil said best approach would be to go with BR.
You can create "after" "update" Business Rule on incident table with the condition when state Changes to Closed and write below script:

var gr=GlideRecord("incident");
    gr.addQuery("parent_incident",current.sys_id);
    gr.query();
    while(gr.next())
    {
        gr.state=7;
        gr.close_code=current.close_code; //mandatory field to close incident
        gr.close_notes=current.close_notes;  ////mandatory field to close incident
        gr.work_notes=current.work_notes;
        gr.update();
    }


Regards,
Mohammad Danish

how to do using only ui action ?? @Mohammad Danis1 

Mohammad Danis1
Giga Guru

@vaishali231 , you can update the UI action script:

var parentIncident = current;  // The current parent incident record

  // Check if the parent incident is being closed
  if (parentIncident.state == 6) { // Assuming "Closed" state has value 6
    // Find all child incidents where the 'parent' field points to the current parent incident
    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent', parentIncident.sys_id);  // Match on parent
    childIncidents.addQuery('state', '!=', 6);  // Don't close already closed incidents
    childIncidents.query();

    // Loop through child incidents and set their state to closed
    while (childIncidents.next()) {
      childIncidents.state = 6;  // Assuming "Closed" state has value 6
      childIncidents.update();  // Save the record
    }

    // Optionally, display a message to the user
    gs.addInfoMessage('Child incidents have been closed.');
  } else {
    gs.addErrorMessage('Parent incident is not yet closed.');
  }


Please share the existing UI acttion script If you can't modify it with above code, I will modify it for you. 🙂

Regards,
Mohammad Danish

MackI
Kilo Sage

HI @vaishali231 

 

You can implement this in following approach----Step 1: Create the UI Action
Using the form you're already on, configure these settings:

Name: Close with Child Incidents
Table: incident
Action name: close_parent_children
Client: (Check this box)
Onclick: closeParentAndChildIncidents()
Condition: gs.hasRole('incident_manager') && current.state != 6 && current.state != 7.... You need to develop a UI Actions script and script include and together ....you can achieve this. 

 

The most effective way to achieve your requirement via Flow Design solution  is to separate the core logic of closing child Incidents and updating work notes into a Flow Designer subflow. The UI Action on the parent Incident will then have a minimal script that simply triggers this subflow, passing the necessary information (like the parent Incident's ID and the work note). This approach promotes reusability, simplifies troubleshooting, and aligns with ServiceNow's low-code/no-code philosophy.

 

If you want a complete solution from me then ..Please kindly mark this your best answer‌🌠‌ .then I will develop the script for you.it will take 1-2 days.

 

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia