- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2023 12:04 AM
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
Solved! Go to Solution.
- 2,280 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2023 11:32 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2023 12:09 AM
Hi Lucky1,
Can you try GlideRecord() api instead of GlideAggregate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2023 12:11 AM - edited 12-23-2023 12:43 AM
Hi,
Use the below script-
Change the parent incident sys_id as per your instance-
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.getUniqueValue());
}
gs.info(childArr)
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2023 12:41 AM
Hello @Lucky1 ,
Please refer the script below and let me know how it works for you.
// Specify the sys_id of the parent incident
var parentIncidentSysID = 'your_parent_incident_sys_id';
// 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 set their state to resolved
while (childIncident.next()) {
childIncident.setValue('state', 6 /* Resolved */);
childIncident.update();
gs.print('Child Incident ' + childIncident.getValue('number') + ' set to Resolved.');
}
} 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.
Regards,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2023 12:42 AM
Hi @Lucky1 ,
In GlideAggregate usually the fields used in orderby or group by will be accessed so you have 2 option
you can add one more line gr.orderBy('sys_id'); or you have to change GlideAggregate to GlideRecord
var gr = new GlideAggregate('incident');
gr.addAggregate('COUNT','parent_incident');
gr.addEncodedQuery('parent_incident!=NULL');
gr.orderBy('parent_incident');
gr.orderBy('sys_id');
gr.query();
while(gr.next()){
gs.print('Incident sys id '+ gr.sys_id);
gs.print("Parent Inc is "+gr.parent_incident.sys_id);
gs.print("Parent Incident State is "+gr.parent_incident.state);
}
Please check and Mark Helpful and Correct if it really helps you.
Regards,
Swathi Sarang