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-28-2022 06:53 AM
Can you please try below
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')
{
return departmentGR.getUniqueValue();
}
else if (departmentGR.description == '')
{
return;
}
else
{
if(departmentGR.parent)
this.walkToDG(departmentGR.parent);
else
return;
}
}
else
{
return;
}
},
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2022 04:45 AM
Hi Saurabh,
thanks for the code, but seems like the same result
Regards,
Jonas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2022 05:35 AM
Can you please run check if there is any infinite loop.
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2022 05:46 AM - edited ‎12-29-2022 06:19 AM
Hi,
PROBABLY I GOT THE POINT.
Your function is called with a bad name.
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