- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 09:46 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 12:11 PM
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:
the script will print the following:
PRJ0010001 -> PRJ0010002 -> PRJ0010003 -> PRJ0010004
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 11:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 11:23 AM - edited 05-19-2025 11:31 AM
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
*** 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 12:11 PM
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:
the script will print the following:
PRJ0010001 -> PRJ0010002 -> PRJ0010003 -> PRJ0010004
Regards,
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 12:12 PM
Hi @Robert H ,
I wrote a recursive function :