Get all the Child Incidents of a parent incident

Lucky1
Tera Guru

Hi all,

 

I have written a script in Scripts Background to get all the child incidents of a parent incident.

My intention is to get the state value of both Parent and Child incidents and then check if the parent incident is resolved, then all the child incidents has to be set to resolved from Scripts background.

 

My code:

var gr = new GlideAggregate('incident');

gr.addAggregate('COUNT','parent_incident');
gr.addEncodedQuery('parent_incident!=NULL');
gr.orderBy('parent_incident');
gr.query();
while(gr.next()){
gs.print("Parent Inc is "+gr.parent_incident.sys_id);
gs.print("Parent Incident State is "+gr.parent_incident.state);

gs.print(gr.sys_id);                // here I am not getting the sys_id of the incident
}

 

 

 

Can someone tell me how to show all the child incidents of a parent incident in an array and to make the state to resolved in scripts background if the parent incident is resolved?

 

 

Thanks in advance.

 

 

Regards,

Lucky

1 ACCEPTED SOLUTION

Hello @Lucky1 ,

 

In the line `for (var x in current)`, the `for...in` loop is used to iterate over all enumerable properties of the object `current`. Here, it's being used to loop through all the fields of the current record (`current`).

 

Regarding the line `var changedMnitorFields = changedFields.filter(function (ele)`, the `filter` function is an array method in JavaScript. It creates a new array with all elements that pass the test implemented by the provided function. In this case, it filters the `changedFields` array based on whether each element (`ele`) is present in the `monitorFields` array. The resulting array (`changedMonitorFields`) contains only the elements that match the monitoring fields.

 

 

Below is the code with added comments:

 

/**
* @Description: Update Child incident based on parent incident
* @monitorFields: Fields to be monitored for updates on the child record
* @changedFields: Fields which are getting updated
* @changedMonitorFields: Array of monitor fields which got changed
**/
(function executeRule(current, previous /*null when async*/) {

  // Fields to be monitored for updates on the child record
  var monitorFields = ['caller_id', 'state', 'impact', 'description'];

  // Fields which are getting updated
  var changedFields = [];

  // Iterate through each field in the current record to identify changes
  for (var x in current) {

    // Add this comment to clarify the purpose of the following line
    // Check if the current field has changed and add it to the changedFields array
    if (current[x] != previous[x]) {
      changedFields.push(x);
    }
  }

  // Create an array of fields that both changed and are in the monitorFields array
  var changedMonitorFields = changedFields.filter(function (ele) {
    return monitorFields.indexOf(ele) != -1;
  });

  gs.addInfoMessage("Checking for changes in parent incident fields...");

  var grIncident = new GlideRecord('incident');
  var query = gs.getMessage('parent_incident={0}^active=true', [current.getUniqueValue()]);

  // Get all the Active child incidents
  grIncident.addEncodedQuery(query);
  grIncident.query();

  // Update each child incident with the changed fields
  while (grIncident.next()) {
    gs.addInfoMessage("Updating child incident: " + grIncident.getDisplayValue());

    changedFields.forEach(function (ele) {
      grIncident[ele] = current[ele];
    });

    grIncident.update();
  }

  gs.addInfoMessage("Child incidents updated successfully.");
})(current, previous);

Please let me know if you still have any doubts or queries 

View solution in original post

22 REPLIES 22

SaurabhGupta_0-1703324831085.png

 


Thanks and Regards,

Saurabh Gupta

Ubada Barmawar
Giga Guru

Here's a revised script that addresses the requirements and incorporates best practices:


var grParent = new GlideRecord('incident');
grParent.addQuery('state', '6'); // Query for resolved parent incidents
grParent.query();

while (grParent.next()) {
gs.print("Parent Incident: " + grParent.number);

var childIncidents = [];
var grChild = new GlideRecord('incident');
grChild.addQuery('parent_incident', grParent.sys_id);
grChild.query();

while (grChild.next()) {
gs.print("- Child Incident: " + grChild.number);
childIncidents.push(grChild.sys_id); // Add child sys_id to array

if (grChild.state != '6') { // Check if child incident is not already resolved
grChild.state = '6'; // Set child incident state to resolved
grChild.update();
gs.print("-- Child incident resolved.");
}
}

gs.print("All child incidents of parent " + grParent.number + " have been processed.");
}

do try it out 

 

Regards,

ubada Barmawar.

darksun50x
Mega Contributor

I am more of an Ai Prompt Engineer so will use ChatGPT to help out;


// Find parent incidents that are resolved
var parentGr = new GlideRecord('incident');
parentGr.addQuery('state', 'resolved');
parentGr.query();

while (parentGr.next()) {
gs.print("Parent Incident is " + parentGr.number);

// Find child incidents related to the current parent incident
var childGr = new GlideRecord('incident');
childGr.addQuery('parent_incident', parentGr.sys_id);
childGr.query();

while (childGr.next()) {
gs.print("Child Incident is " + childGr.number);

// Set the state of child incidents to "resolved"
childGr.state = 'resolved';
childGr.update();
gs.print("Child Incident state set to resolved");
}
}


This script first queries for parent incidents that are resolved. For each resolved parent incident, it then queries for child incidents related to that parent incident and sets their state to "resolved". Note that you need to replace 'incident' with the actual table name of your incident table if it's different.

Also, consider adding additional error handling and logging based on your specific requirements and coding standards.

darksun50x
Mega Contributor

This should fix it plus the updates- 

To avoid updating comments for child incidents, you need to explicitly exclude the comments when updating the child incidents. Here’s the modified script:

// Find parent incidents that are resolved
var parentGr = new GlideRecord('incident');
parentGr.addQuery('state', 'resolved');
parentGr.query();

while (parentGr.next()) {
gs.print("Parent Incident is " + parentGr.number);

// Find child incidents related to the current parent incident
var childGr = new GlideRecord('incident');
childGr.addQuery('parent_incident', parentGr.sys_id);
childGr.query();

while (childGr.next()) {
gs.print("Child Incident is " + childGr.number);

// Set the state of child incidents to "resolved" excluding comments
childGr.setValue('state', 'resolved');
childGr.setWorkflow(false); // Disable workflow to prevent business rules and notifications
childGr.update();
gs.print("Child Incident state set to resolved");
}
}

In this version, I replaced childGr.state = 'resolved'; with childGr.setValue('state', 'resolved');. Additionally, I added childGr.setWorkflow(false); to temporarily disable workflow execution, preventing unintended business rule and notification triggers.

Sid564
Tera Contributor

Hi 
Use below script

var grParentInc = new GlideRecord('incident');
grParentInc.addQuery('sys_id', current.sys_id); // Substitute actual parent incident sys_id here
grParentInc.query();

if (grParentInc.next()) {
var parentState = grParentInc.getValue('state');
// Get all child incidents of the parent
var grChildIncs = new GlideRecord('incident');
grChildIncs.addQuery('parent_incident', current.sys_id);
grChildIncs.query();
while (grChildIncs.next()) {
var childState = grChildIncs.getValue('state');
// Check if child state needs to be updated
if (parentState == 'resolved' && childState != 'resolved') {
grChildIncs.setValue('state', 'resolved');
grChildIncs.update();
}
}
}