How to find the highest incident parent

Priya75
Tera Contributor

Hi All,

 

I am trying to make a ui Action and want to find the highest most parent of the incident.

 

so i am trying to find the parent incident, For Example:- if INC-0 has a parent INC-2 and INC-2 has a parent INC 3, I want to fetch the sysid of the highest parent in the chain. the  script does not seems to be working.

 

function openHierarchy() {
incident1 = current.sys_id;
gs.info("parent" + incident1);
//if (parent!=""){
var gr = new GlideRecord("incident");
gr.addQuery('sys_id', incident1);
gr.query();
if (gr.next()) {
gs.info("inside if PR");
var highest_parent = getHighestParent(gr);
if (highest_parent) {
gs.info("found" + highest_parent.number);
} else {
gs.info("inside else PR");
gs.info("not found" + highest_parent.number);

}
}

function getHighestParent(gr) {
if (gr.parent_incident.getRefRecord()) {
gs.info("inside if PR22222222")
gs.info("refrence record"+gr.parent_incident.getRefRecord());
return getHighestParent(gr.parent_incident.getRefRecord());

} else {
gs.info("inside else PR22222222")
return gr;
}

}

 

2 REPLIES 2

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Priya75 ,

 

Few issues that i found in the script are:

1. Trailing curly brace was missing in the first function

2. Usage of getRefRecord() inside if condition, if the reference field has an empty value, getRefRecord() doesn't throw any error but it does return a GlideRecord object, i've replaced it with getValue()

3. Changed the parameter name for the function getHighestParent to avoid confusion.

4. There's no function call

 

Please use the below script:

openHierarchy();
function openHierarchy() {
    incident1 = current.sys_id;
    gs.print("parent" + incident1);
    //if (parent!=""){
    var gr = new GlideRecord("incident");
    gr.addQuery('sys_id', incident1);
    gr.query();
    if (gr.next()) {
        gs.print("inside if PR");
        var highest_parent = getHighestParent(gr);
        if (highest_parent) {
            gs.print("found " + highest_parent.number);
        } else {
            gs.print("inside else PR");
            gs.print("not found" + highest_parent.number);

        }
    }
}

function getHighestParent(incGR) {
    if (incGR.getValue('parent_incident')) {
        gs.print("inside if PR22222222")
        gs.print("refrence record" + incGR.parent_incident.getRefRecord());
        return getHighestParent(incGR.parent_incident.getRefRecord());

    } else {
        gs.print("inside else PR22222222")
        return incGR;
    }
}

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks,

Karan

Rahul Kumar17
Tera Guru

Hi,

function openHierarchy() {
  var incident1 = current.sys_id;
  gs.info("parent: " + incident1);
  
  var gr = new GlideRecord("incident");
  gr.addQuery('sys_id', incident1);
  gr.query();
  
  if (gr.next()) {
    var highest_parent = getHighestParent(gr);
    
    if (highest_parent) {
      gs.info("found: " + highest_parent.number);
    } else {
      gs.info("highest parent not found");
    }
  }
}

function getHighestParent(gr) {
  if (gr.parent_incident.getRefRecord()) {
    return getHighestParent(gr.parent_incident.getRefRecord());
  } else {
    return gr;
  }
}

openHierarchy();

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar