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

Lucky1
Tera Guru

Thank you everyone,

 

Thanks to ANiket and Saurabh and Swati,

 

Actually, I want to print all the child incidents that are associated with a parent incident into an array and print that array.

 

 

Regards,

Lucky

 

Hi @Lucky1 
You can mark any answer as accepted.
But here you are not doing any sort of aggregation.
You want sys_id of child incidents based on parent. No aggregation.
GlideAggregate will make sense if you want to know the count of all child incidents.



Thanks and Regards,

Saurabh Gupta

Hello Saurabh,

 

I have requested on how to get all the child incidents of a parent incident.

So, for example, if there are 3 child incidents for an Incident, then I want to push all the 3 child incidents number but not Sys_id to an array.

 

Can you tell me how can i do this?

 

 

Regards,

Lucky

Hello @Lucky1 ,

 

Can you try with the below script and let me know if this meet with your requirement or not.

// Specify the sys_id of the parent incident
var parentIncidentSysID = 'your_parent_incident_sys_id';

// Create an array to store child incident numbers
var childIncidentNumbers = [];

// Query child incidents based on the parent incident
var childIncident = new GlideRecord('incident');
childIncident.addQuery('parent_incident', parentIncidentSysID);
childIncident.query();

// Check if the parent incident is resolved
var parentIncident = new GlideRecord('incident');
if (parentIncident.get(parentIncidentSysID) && parentIncident.getValue('state') == 6 /* Resolved */) {
    // Iterate through child incidents and push their numbers to the array
    while (childIncident.next()) {
        childIncidentNumbers.push(childIncident.getValue('number'));
    }

    // Print or use the array as needed
    gs.print('Child Incident Numbers: ' + childIncidentNumbers.join(', '));
} else {
    gs.print('Parent Incident is not resolved.');
}

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

Hi ANiket,

 

I tried but it is giving error.

Lucky1_0-1703324427975.png

 

My code:

var arr = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('parent_incident!=NULL');
gr.addQuery('parent_incident',current.sys_id);
//gr.get(parent_incident.sys_id);
gr.query();
while(gr.next()){

arr.push(gr.getValue(number));
gs.log("ArrayNow is "+arr);

gr.comments = current.comments;
gr.comments = arr.toString();
gr.update();
}