- 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-01-2023 09:01 PM
Hi @Lak,
As per the details given, the department hierarchy flow from top to bottom and user Mike belong to ABC in sys_user table and ABC is parent of BCD , BCD parent of CDE, CDE parent of XYZ , if this is correct understanding then use below code to get Nth level down department value for any given user's department
Created department hierarchy
In my PID, user [ andrew.jackson] 's department = ABC, and below script will return the XYZ.
Note: This code will return the Nth ( down ) level department name for such hierarchy.
var deptArray = [];
var count =0;
var grUser = new GlideRecord("sys_user");
grUser.addQuery("user_name","andrew.jackson"); // replace the use name
grUser.query();
if(grUser.next()){
//call the function with user's departmert
getLastLevel(grUser.department);
//display the last level department name from array
gs.info("deptArray->"+deptArray[deptArray.length-1]);
}
function getLastLevel(dept){
var grDept = new GlideRecord("cmn_department");
// check if department is added as parent of any other department
grDept.addQuery("parent",dept);
grDept.query();
// if any other department found
if(grDept.next()){
// add the department name for given parent department
deptArray.push(grDept.name);
// check for next down level for department name
getLastLevel(grDept.sys_id);
}
}
Executed this code in fix-script for result
-Thanks,
AshishKMishra
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-01-2023 09:32 PM
Hi @AshishKM ,
Thanks for your response here and much appreciated for the code you have shared.
Can the same code be used in the advanced reference qualifier so, that for any user located from.sys_user table it would display the bottom most department.
regards,
Lakshmi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 09:37 PM
Are you working on any catalog item and looking for bottom most department for selected user, if yes , you can create a script include and call the function via onChange of userId field.
Give the more details along with screen shot , i will try to replicate the same and share.
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-01-2023 09:53 PM - edited 12-01-2023 09:57 PM
Hi @AshishKM ,
I am working on with successfactors integration with servicenow and have successfully pulled the department data from successfactors for users but not sure which level of department it is currently but the ask is to populate the nth level department and be shown at the user level hence I am asking if a advanced reference qualifier will suffice the purpose to achieve the output expected.
regards,
Lakshmi