Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to get parent ci

pradeepgupta
Giga Expert

Hi All,

There are CI A,B,C & D with below relationship.

kb.png

I have to write a function in such a way that if I give ci D than function should return a result as array or   all the ci (A,B,C,D).

1 ACCEPTED SOLUTION
13 REPLIES 13

bashashaik
Giga Contributor

Hi Pradeep,



getRelation(CIid , relationlength ) // relationLength mean how much dependence you want .. ex: 2 means parent's parent



function getRelation()


{


        var relationShip = '';




    for(var i=0;i<=relationLength ; i++)


        {


                  relationShip += 'parent.';


        }


        Eg. if for length 2 it give value as parent.parent



        var gr = new GlideRecord(TableName);


        gr.addQuery('sys_id',CIID);


        gr.query();


        while(gr.next())


        {


                  var CIrelatedTo2ndparent = gr.getValue(relationShip);   // it gives you the Exact CI


        }


}


PeterWiles
Kilo Sage

You will want to create a recursive lookup. So something like the below. I have not tested it and it all depend on what relationships you have set-up. I would also add in some data checking for infinite loops and ensure values are all set.




var finalArr = new Array();


getRelationships(current.sys_id);



function getRelationships(ci)


{


var []



    //Loopup relationship table to see what it's dependant is.


var rel = new GlideRecord("");


rel.addQuery("parent",ci);


rel.addQuery("type", <<INSERT Relationship type here>>);


rel.query();



if(rel.next)


{


    finalArr.push(rel.sys_id)


  getRelationships(rel.sys_id);


}



}


Hi Peter Wiles,



Recursive function is not working.


It calls function only once.