Recursive script include returning "undefined"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2022 06:36 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2022 05:55 AM
@Jonas VK Please try below code this should definitely fix the issue
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2022 06:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 12:42 AM
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:
logs:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2023 02:04 AM - edited ‎01-02-2023 02:33 PM
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