The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Script subquery: recursive parent names till root level

sathwikputhrao
Kilo Explorer


Hi,

 

I am in need of help for a script to fetch the parent name of that CI on its sys_id, and in turn parent of that parent name i.e recursive parent names for a CI till the root level.

Basically its a tree of the parent names from the CI parent till its root parent.This parent field is in the core_company table.

This script basically avoids multiple core_company web service calls to get the parent company names till the root level.

I am just able to get the parent name of the CI and not able to recursively get the parent's parent name.

 

Kindly help.

 

below code I tried just returns me the parent name but I dont know how to move further:

 

function getParent(req)
{

            var parentRecord = '';
            var lookup= new GlideRecord('core_company');
            lookup.addQuery(name,req);
            lookup.query();
            while (lookup.next()) {
                              parentRecord= lookup.parent;
                                                                    }

}

1 REPLY 1

Harish Murikina
Tera Guru

Hi Sathwik,



                                The below code can works.



var parent = getParent(current.request);






function getParent(request)


{




  var return_parent_name = '';


  var parent_name = request;





  var lookup= new GlideRecord('core_company');


  lookup.addQuery(name,parent_name);


  lookup.query();


  if(lookup.next())


  {


  parent_name = lookup.parent;


  return_parent_name = getParent(parent_name);       // this is recursive functionality       the function will get call untill last most parent record find


  }




  if( return_parent_name !='' ||   return_parent_name != 'undefined')


  {



  return   return_parent_name; // It will return most parent record


  }


  else


  {


  return parent_name; // If we don't have any parent it will return what you passed in parameter above to call function


  }




}




Regards,


Harish.