Retrieving Hierarchal data from a table

Anubhav24
Mega Sage
Mega Sage

Hi All,

 

I have a table where each record has its parent defined until it reaches the topmost parent, similar to a family tree where every child has its own set of parent and till which level the parents are there is specified on the child record.

Now I get the first child record reference say C1 , now I can access the parent say P1 simply by referring to the field i.e. 'u_parent' as this field is defined on the record using the gliderecord object. Now if I have to access P1' parent it will be like C1.P1.u_parent and so on for each child's parent. and these levels can be dynamic some may have data populated till only two levels of parents and some may have 5 levels of parents.

 

What I did was I got the level from the first child i.e. reference record say level 4 which means till 4 levels I need to go down to get all the details.

Now what I did was I stored the first reference i.e. to child in a variable as a string i.e. var fam = 'gr.u_name.u_parent'.

Now I run a loop till the maximum level and while this loop is running every alteration I am just adding 'u_parent' i.e.

Loop 1 fam = fam+'.u_parent' which will result in 'gr.u_name.u_parent.u_parent' and every alteration I try to obtain the value , this is where the problem arises since this dot walking is converted to a string variable it will not obtain the value instead it will print the string as it is i.e. gr.u_name.u_parent.u_parent .

 

Is there any other way to retrieve these values or to again convert these strings to get the original value or will gr.getValue() function work?

 

Thanks in advance

1 ACCEPTED SOLUTION

Hello @Anubhav24 ,

 

Ok, so you literally just want to print the project hierarchy with a script?

Then please use this:

 

var list = new GlideRecord('pm_project');
if (list.get('number', 'PRJ0010004')) { // REPLACE WITH THE NUMBER OF YOUR CHILD PROJECT

	var currentProject = list,
		projectHierarchy = [];
	do {
		projectHierarchy.push(currentProject.number.toString());
		currentProject = currentProject.parent;
	} while (currentProject);
	
	projectHierarchy.reverse();
	gs.info(projectHierarchy.join(' -> '));
	
}

 

Given the following projects:

 

RobertH_0-1747681777509.png

 

the script will print the following:

 

PRJ0010001 -> PRJ0010002 -> PRJ0010003 -> PRJ0010004

 

Regards,

Robert

 

 

 

View solution in original post

5 REPLIES 5

Robert H
Mega Sage

Hello @Anubhav24 ,

 

You mention a lot about your hierarchical data and what you are doing. But you don't mention what you want to achieve, i.e. what is your requirement?

 

Do you want to be able to report on or filter all records that belong to a certain top level parent?

Or do you want to know what is the top level parent for a given child record?

Something else?

 

Regards,

Robert

Hi @Robert H ,

What I want to achieve is GGF -> GF -> Father -> Child I want the data in this order.

here is the script I am trying , the error occurs the moment I concatenate something while dot-walking the system starts treating it as a string value

var list = new GlideRecord("pm_project");
list.addQuery("sys_id",'1eccc5596b69aa100c479974ab63fb8c');
list.query();
while (list.next()){
    gs.info('Record is '+ list.getDisplayValue());
   hierarchylevel = list.u_hierarchy_element_unit.u_hierarchy_level.getDisplayValue().substr(-1);
    var parent = 'list.u_hierarchy_element_unit';
    for (var i =0; i<4;i++)
    {
    parent +='.u_parent';
    gs.info(parent.toString());
    }
 
OP:
*** Script: acc15113f7a9f5d0ec1c41b84851e0b2.u_parent
*** Script: acc15113f7a9f5d0ec1c41b84851e0b2.u_parent.u_parent
*** Script: acc15113f7a9f5d0ec1c41b84851e0b2.u_parent.u_parent.u_parent
*** Script: acc15113f7a9f5d0ec1c41b84851e0b2.u_parent.u_parent.u_parent.u_parent

Hello @Anubhav24 ,

 

Ok, so you literally just want to print the project hierarchy with a script?

Then please use this:

 

var list = new GlideRecord('pm_project');
if (list.get('number', 'PRJ0010004')) { // REPLACE WITH THE NUMBER OF YOUR CHILD PROJECT

	var currentProject = list,
		projectHierarchy = [];
	do {
		projectHierarchy.push(currentProject.number.toString());
		currentProject = currentProject.parent;
	} while (currentProject);
	
	projectHierarchy.reverse();
	gs.info(projectHierarchy.join(' -> '));
	
}

 

Given the following projects:

 

RobertH_0-1747681777509.png

 

the script will print the following:

 

PRJ0010001 -> PRJ0010002 -> PRJ0010003 -> PRJ0010004

 

Regards,

Robert

 

 

 

Hi @Robert H ,

 

I wrote a recursive function :

 

function getAllParents(child,parents)
{
var parentList=parents || [];
var grHierarchy = new GlideRecord('u_hierarchy_elements');
if(grHierarchy.get(child))
{
if (grHierarchy.u_parent)
{
 
var parent = grHierarchy.u_parent.getDisplayValue();
gs.info('Parent is '+parent);
 
parentList.push(parent.toString());
gs.info('ParentList '+parentList.toString());
this.getAllParents(grHierarchy.u_parent,parentList);
 
 
}
else
gs.info('Inside else statement '+parentList.toString());
return parentList;
}
 
Is this fine performance wise ? can some one review it please