Recursive script include returning "undefined"

Jonas VK
Tera Guru

Hi All,
I am running into an issue with my script include. I use this to go through the departments relations until i reach a specific department. The issue is it always returns undefined. But when i debug it and check the console. it does find the correct department, but afterwards it keeps searching and returns undefined.SO my question is more why does it keep running after it finds the correct department or is my going to undefined afterwards.


See below the script:

getDGDepartment: function(){
		var userDepartment = gs.getUser().getRecord().getValue('department');
		if(userDepartment){
			var department = this._walkToDG(userDepartment);
		} else {
			gs.error('DIGITFilterRecordsByDG.getDGDepartment: No Department found for current user.');
			return;
		}
		return department;
	},
	
    //This script will walk trough all the departments untill it ends at the DG deparmtent
    //This is doing via a recursive function
    _walkToDG: function(department)
	{
        var DGDepartment = "";
        var departmentGR = new GlideRecord('cmn_department');
        departmentGR.get(department);
        if (departmentGR.description == 'DG')
		{
            gs.info("true " + departmentGR.name + " " + departmentGR.description + " " + departmentGR.sys_id);
            DGDepartment = departmentGR.sys_id;
			return DGDepartment;

        } 
		else if (departmentGR.description == '')
		{
            return;
        } 
		else
		{
            gs.info(departmentGR.name + " " + departmentGR.sys_id);
            //Call the function itself again to restart the process of looking for the department
            this.walkToDG(departmentGR.parent);
        }
    },

 

If anybody could potentially help me out it would be highly appreciated.

Regards,
Jonas

8 REPLIES 8

jaheerhattiwale
Mega Sage
Mega Sage

@Jonas VK Please try below code this should definitely fix the issue

 

getDGDepartment: function(){
        var userDepartment = gs.getUser().getRecord().getValue('department');
        if(userDepartment){
            return this._walkToDG(userDepartment);
        } else {
            gs.error('DIGITFilterRecordsByDG.getDGDepartment: No Department found for current user.');
            return;
        }
    },
   
    //This script will walk trough all the departments untill it ends at the DG deparmtent
    //This is doing via a recursive function
    _walkToDG: function(department)
    {
        var DGDepartment = "";
        var departmentGR = new GlideRecord('cmn_department');
        if(departmentGR.get(department)){
        if (departmentGR.description == 'DG')
        {
            gs.info("true " + departmentGR.name + " " + departmentGR.description + " " + departmentGR.sys_id);
            DGDepartment = departmentGR.sys_id;
            return DGDepartment;

        }
        else if (departmentGR.description == '')
        {
            return;
        }
        else
        {
            gs.info(departmentGR.name + " " + departmentGR.sys_id);
            //Call the function itself again to restart the process of looking for the department
            return this.walkToDG(departmentGR.parent);
        }
        }
    },
 
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @Jonas VK 

 

I tried below code in my PDI and is working fine.

 

	getDGDepartment: function(){
		var userDepartment = gs.getUser().getRecord().getValue('department');
		if(userDepartment){
			var department = this._walkToDG(userDepartment);
		} else {
			gs.error('DIGITFilterRecordsByDG.getDGDepartment: No Department found for current user.');
			return;
		}
		return department;
	},

		//This script will walk trough all the departments untill it ends at the DG deparmtent
		//This is doing via a recursive function
		_walkToDG: function(department)
	{
		var DGDepartment = "";
		var departmentGR = new GlideRecord('cmn_department');
		departmentGR.addQuery('sys_id',department);
		departmentGR.query();
		if(departmentGR.next())
		{
			if (departmentGR.description == 'DG')
			{
				DGDepartment= departmentGR.getUniqueValue();
			} 
			else
			{
				if(departmentGR.getValue('parent'))
					DGDepartment=this._walkToDG(departmentGR.getValue('parent'));//This function was wrongly called as walkToDG
			}
		}
		return DGDepartment;
	},

Thanks and Regards,

Saurabh Gupta

The script seems to indeed run all the way to the correct department, however when i do a gs.info at "if descriptions == DG" it finds the ID but it doesn't return it for some reason for me. the walking to it does work fine and on my pdi it also seems to work.
New script:

JonasVK_1-1672648939799.pngJonasVK_2-1672648957300.png

 


logs:

JonasVK_0-1672648894806.png

 

Hi,
Just use one return statement wherever you are not returning anything do not use return statement.
One more thing use below line

//Use below line
DGDepartment=this._walkToDG(departmentGR.getValue('parent'));

//Do not use "this._walkToDG(departmentGR.getValue('parent'));" alone save this in your variable as you are returning something

 

 


Thanks and Regards,

Saurabh Gupta