Script subquery: recursive parent names till root level
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2014 04:12 AM
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;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2014 04:40 AM
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.