Find children, grandchildren of an incident ServiceNow

Abhijith322
Tera Contributor

Hello Folks,

I'm looking for an optimized code to find children, grandchildren etc. of a parent incident. Something like an advanced version of various search algorithms(DFS, BFS etc.)

 

Abhijith322_0-1719572746419.png

 

 

Finding the first level child would be easy using a simple GlideRecord query. But it is not the case when there are multiple grandchildren under different parent. If someone feels this is a bad requirement, please read below.

 

 Most people might think there is no likelihood of this particular use case due to the fact that one incident will always be the parent and others will be its child( no concept of grandchildren). I'm actually looking for a logic to implement the same in my custom application. Hence, no questions like, 'Why can't you resolve the incident and create a problem instead'?

 

Any help is much appreciated.

4 REPLIES 4

Mark Manders
Mega Patron

What are you looking for? Not saying it is a bad requirement, because there isn't a requirement. How do you need this to be populated? Do you need a related list on the parent, showing all children and their children? Do you need a report? Do you need to just query to them to do something with them? 

Please explain your logic, because you are stating that there is a use case, but you don't make it. You only state that it exist and that you don't want a problem to be the solution. Your requirement isn't shared.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

@Mark Manders , I'm looking for an array that holds sysIDs of all children and grandchildren to be included in a REST payload.

Just some script to start from (you need to define how to find the parent sys_id, added as example in the last lines), but the function should get you all the related incidents to put in the payload:

function getAllRelatedIncidents(parentIncidentSysId) {
    var relatedIncidents = [];
    
    function fetchChildIncidents(parentSysId) {
        var childIncidents = [];
        var incidentGR = new GlideRecord('incident');
        incidentGR.addQuery('parent', parentSysId);
        incidentGR.query();
        
        while (incidentGR.next()) {
            var sysId = incidentGR.getValue('sys_id');
            childIncidents.push(sysId);
            var descendants = fetchChildIncidents(sysId);
            childIncidents = childIncidents.concat(descendants);
        }
        
        return childIncidents;
    }
    
    relatedIncidents = fetchChildIncidents(parentIncidentSysId);
    return relatedIncidents;
}

var parentIncidentSysId = 'some_sys_id_here';
var allRelatedIncidentSysIds = getAllRelatedIncidents(parentIncidentSysId);
gs.print('Related Incident Sys IDs: ' + allRelatedIncidentSysIds.join(', '));

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

briannice
Kilo Sage

Hi @Abhijith322 

 

I would try to solve this problem using an iterative solution and not a recursive / backtracking solution (like DFS or BFS). I have added a code sample below.

 

var rootIncidentId = "1d6c5c0f473a4210d4ff9889316d431e";

var incidentIds = [rootIncidentId];
var cursor = 0;

while (cursor < incidentIds.length) {
	var parentIncidentId = incidentIds[cursor];
	var childIncidentGr = new GlideRecord("incident");
	childIncidentGr.addQuery("parent", parentIncidentId);
	childIncidentGr.query();
	while (childIncidentGr.next()) {
		incidentIds.push(childIncidentGr.getUniqueValue());
	}
	cursor++;
}

gs.print(incidentIds);