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

Hello @Lucky1 ,

 

There are several mistakes present in the code wait I will give you a code so just check with that and let me know you views.

Hello @Lucky1 ,

 

Please try with this code:

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

while (gr.next()) {
    arr.push(gr.getValue('number'));
}

// Log the array content for verification
gs.log("ArrayNow is " + arr.join(', '));

// Update current incident's comments with child incident numbers
if (arr.length > 0) {
    current.comments = "Child Incident Numbers: " + arr.join(', ');
    current.update();
}

Hi ANiket,

 

Thank you.

It's working.

 

I have written a BR before update on Incident form with condition Additional comments changes.

My intention is to update the Additional comments to Child incidents also when Parent Incident Additional comments are updated.

But here your code is working for Parent Incident, but the same comments are being updated to Child incidents.

 

Can you help me out in this last- scenario?

 

Regards,

 

Lucky

Hello @Lucky1 ,

 

Also the code which you said was not working and throwing an errors , so that you ran in background script or?

If yes then you cannot use currect object in the background script, so let me know so that I can provide you the updated code accordingly.

Hi,
You can use as below-

var parent_inc_sys_id='d85fe6f707cfb9105295f19d7c1ed09e';//sys_id of parent incident
var childArr=[];
var gr = new GlideRecord('incident');
gr.addQuery("parent_incident",parent_inc_sys_id)
gr.query();
while(gr.next()){
childArr.push(gr.getValue('number'));
}

gs.info(childArr)

Thanks and Regards,

Saurabh Gupta