- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 08:01 PM - edited 12-01-2023 10:15 PM
For any given user(Mike), We would need to show the bottom most department(XYZ).
Note: User could be associated to any department in the hierarchy, any help or suggestions
User: Mike
Department:
--ABC
--BCD
--CDE
--XYZ
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2023 05:39 AM - edited 12-02-2023 06:05 AM
Ok , got it, you can use this code. you might required some minor adjustments for input and output parameters.
created a new column on my PDI for this Nth level and used the calculated value to display Nth value based on current department.
updated the script code for this
you can copy exact code in calculated value, its generic as its taking the current record department for nth level
(function calculatedFieldValue(current) {
var deptArray = [];
getLastLevel(current.department);
function getLastLevel(dept){
var grDept = new GlideRecord("cmn_department");
grDept.addQuery("parent",dept);
grDept.query();
if(grDept.next()){
deptArray.push(grDept.sys_id);
getLastLevel(grDept.sys_id);
}
}
return deptArray[deptArray.length-1];
})(current);
result
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 12:35 PM - edited 12-03-2023 12:36 PM
Hi @AshishKM ,
The challenge with calculated field is that I don't have an i icon to refer the details of that filed and which will be available only through reference field hence I have tried calling the script include through qualifier reference qualifier but unfortunately it isn't working.
Regards,
Lakshmi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 06:19 AM
@Lak , let me know if you can share the screen and we are review the code.
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 06:22 AM
Hi @AshishKM , Thanks for the logic provided here, I was able to manage and built the same into my existing script include function and I am able to transform the data and was able to get the nth level department. Thanks again Ashish for the support you have enabled.
regards,
Lakshmi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 06:37 AM
@Lak,thanks for replying and confirming, Please accept solution and close this thread for others.
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 07:36 AM
HI @Lak ,
I trust you are doing great.
Here's a template to modify the script for integration:
// Script Include Template
var BottomMostDepartment = Class.create();
BottomMostDepartment.prototype = {
initialize: function() {
},
getBottomMostDepartment: function(userId) {
var deptArray = [];
var grUser = new GlideRecord("sys_user");
grUser.addQuery("user_name", userId); // Replace with actual user field
grUser.query();
if(grUser.next()){
this.getLastLevel(grUser.department, deptArray);
return deptArray[deptArray.length - 1];
}
return ''; // Return empty if no department found
},
getLastLevel: function(dept, deptArray) {
var grDept = new GlideRecord("cmn_department");
grDept.addQuery("parent", dept);
grDept.query();
if(grDept.next()){
deptArray.push(grDept.name);
this.getLastLevel(grDept.sys_id, deptArray);
}
},
type: 'BottomMostDepartment'
};
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi