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

test in Scripts - Background. I don't see logic to check for 'circular' configuration:

R1 parent: C1

C1 parent: R1

Garbage data, can result in infinite loop.