- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 03:43 PM
Hi Everyone,
we are trying to create a recursive function to get the one up value for the departments until the parent is found, the function was setup as below but the function returns undefined value, i've added logs just before the return to verify if there's any data and it does see the sys_id of the record but when returned we receive undefined value. Hoping to get some insights, thank you everyone
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 12:46 AM
Hi @edosky ,
I have revised the code, check this out.
var oneUp = getOneUp(gr);
function getOneUp(rec) {
var div;
var record = new GlideRecord('cmn_department');
record.get(rec);
if (record.parent.toString() == '') {
gs.print("Found top-level department: " + record.sys_id);
div = record.sys_id.toString();
return div; // Return top-level department sys_id
} else {
return getOneUp(record.parent); // Return result from recursive call
}
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2025 08:26 PM
the below sample script include is for fetching reportees using recursion
please enhance as per your requirement
var UserUtils = Class.create();
UserUtils.prototype = {
initialize: function() {},
getAllReportees: function(managerSysId) {
var allReports = [];
// Start collecting from the given manager
this.collectReports(managerSysId, allReports);
return allReports.toString();
},
// Recursive function to collect reports
collectReports: function(managerId, allReports) {
var directReports = this.getDirectReports(managerId);
while (directReports.next()) {
allReports.push(directReports.sys_id.toString());
this.collectReports(directReports.sys_id, allReports); // Recursively collect indirect reports
}
},
// Function to get direct reports
getDirectReports: function(managerId) {
var gr = new GlideRecord('sys_user');
gr.addQuery('manager', managerId);
gr.query();
return gr;
},
type: 'UserUtils'
};
call it like this
new UserUtils().getAllReportees('managerSysId');
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:09 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 12:46 AM
Hi @edosky ,
I have revised the code, check this out.
var oneUp = getOneUp(gr);
function getOneUp(rec) {
var div;
var record = new GlideRecord('cmn_department');
record.get(rec);
if (record.parent.toString() == '') {
gs.print("Found top-level department: " + record.sys_id);
div = record.sys_id.toString();
return div; // Return top-level department sys_id
} else {
return getOneUp(record.parent); // Return result from recursive call
}
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2025 04:44 PM
Hi @Ankur Bawiskar @Runjay Patel ,
Thank you both for your responses, I had checked with our team and their proposed solution was same with what Runjay mentioned above, the recursive was returning as undefined because the else condition does not return any results.