Creating a Recursive Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:06 AM
Can anybody advise what script to use to be able to help build a report based on the users "Level 1" Company.
We have over 1000 companies that can be selected for our users, however we only have 5 "Level 1" companies. The remaining companies will then have a parent company that leads into the 5 “Level 1” companies.
I would like to run a report that uses a recursive script that runs through the parent companies up the levels until it finds the parent that is a “Level 1” company. Note that they are identified by a tick box that is ticked if they are a “Level 1” company.
The complexity comes in that some users could have their company as the “level 1” company, others it maybe the parent company, and others may need to go through 2 or more levels of parent companies before it reaches the “Level 1” company. Ie
|
User |
Company |
Parent Company |
Parent Company |
Parent Company |
|
User 1 |
Level 1 Company |
|
|
|
|
User 2 |
Level 2 Company |
Level 1 Company |
|
|
|
User 3 |
Level 4 company |
Level 3 company |
Level 2 Company |
Level 1 Company |
- Labels:
-
Reporting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 07:39 AM
Great thank you for the help I will give this a try
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 08:30 AM
Ideally, you'll maintain this on the user record (or whatever the base record is) updated on company parent change or company change on the user and not calculate this on the fly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2020 08:57 AM
Mark, I had almost similar requirement where I had to get to a certain role (like a VP) in the user hierarchy and during the troubleshooting process, I came across this post.
In your script include, there is no code to avoid infinite loop in the else block before calling the recursive function. Will it not go into an infinite loop if it doesn't find the desired value?
In the script include I wrote, I had a counter that would increment every time it went into else and break out of the loop if it is 10. Inspite of that, it still is going into an infinite loop. Can you please help? What is wrong with my script?
Here is my script include:
fetchVP: function(usr_sys_id){
var counter = 0;
var vp='';
vp= this.runAgain(usr_sys_id,counter);
return vp;
},
runAgain: function(usr,counter){
var leader = '';
var gr = new GlideRecord('sys_user');
gr.get(usr);
if(gr.title.toLowerCase().startsWith("vp")){
leader = gr.sys_id;
gs.log('current inside if condition vp'+leader+ '- counter: '+counter);
}
else {
counter++;
gs.log(counter);
if(counter>10){
leader =gs.getProperty('Not Valid User');
return leader.toString();
}
gs.log('current inside else condition before calling recursive function');
var tt = gr.manager.toString();
this.runAgain(tt,counter);
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:48 AM
Yes they are all in the core company table.
Field Label is 'Parent'
Tick box is 'u_level_1'
Yeah true/false field
Thank you for your help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2018 04:56 AM
Just checking on on this one. Has this question been answered or is there more information I can help you with? If it's been answered, please mark the answer above as the correct one so people know this has been taken care of. Thanks!
