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

Aniket Chavan
Tera Sage
Tera Sage

Hello @Lucky1 

To address your query about updating the comments and worknotes for child incidents, I recommend referring to the following links for detailed information. Feel free to review the content, and if you have any remaining doubts or further queries, please don't hesitate to let me know.

 

 

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.

 

Thanks,
Aniket

Hi ANiket,

 

for (var x in current){

What does that line mean in the code of second link you pasted?

I know for loop using like, for(var i=0;i<arr.length;i++)

 

also,

this line of code:

var changedMnitorFields = changedFields.filter(function (ele)

So, I am confused to understand fileter and inside brackets function (ele)

 

 

Regards,

Lucky

 

 

 

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