Recursive function returns undefined

edosky
Tera Expert

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

 

getOneUp: function(rec) {
        var div;
        var record = new GlideRecord('cmn_department');
        record.get(rec);
        if (record.parent.toString() == '') {
            gs.info("test getoneup record: " + record.sys_id);
            div = record.sys_id.toString();
            return div;
        } else {
            this.getOneUp(record.parent);
        }
    }
1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@edosky 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@edosky 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

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.