- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 04:45 PM
Hello,
I need to find the "active" department that users are part of. I tried the following code but the answer is null. Could someone help me ?
var deptArr = gs.getUser().getUserByID('sys_id').getDepartmentID();
var myDept = [];
myDept.push(deptArr);
// gs.info("depts are " + typeof myDept+ "users dept are :" +myDept);
for (var i = 0; i < myDept.length; i++) {
var deptGr = new GlideRecord('cmn_department');
deptGr.addQuery('u_active', true);
deptGr.addQuery('sys_id', myDept[i]);
deptGr.query();
if (deptGr.next()) {
var activeDept = [];
activeDept.push(deptGr);
gs.info(activeDept);
}
}
e
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 03:10 PM
Found the solution on my own. Thanks to you all.
function getDepartment() {
var user = new GlideRecord('sys_user');
user.get(sys_id);
var dept_list = '';
var departments = [];
departments = user.department.split(',');
// gs.info(departments);
for (var i = 0; i < departments.length; i++) {
dptl = new GlideRecord('cmn_department');
dptl.get(departments[i]);
if (dptl.u_active) {
dept_list += dptl.code + ',';
dept_list.push(dptl.code);
gs.info(dept_list);
}
}
return dept_list;
}
getDepartment();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 05:21 PM
Hi, in OOB ServiceNow the relationship between cmn_department and sys_user is 1 to 1.
Meaning a user has only 1 department relationship and this is defined in their sys_user record.
I see you have added a custom 'u_active' field against department,
this would not impact the existing 1 to 1 department to user relationship and the result would be a sys_user.department that is either active = true or active = false;
but this customization will not allow you to have more than 1 department relationship, without a M2M table to define\contain this information and I do not believe that this exists OOB.
Perhaps you can clarify your configuration and requirements?
OOB the only script you need to return the sys_id of your users department is
gs.getUser().getDepartmentID();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 08:04 PM
Can you try this instead?
var deptArr = gs.getUser().getDepartmentID();
var myDept = [];
myDept.push(deptArr);
//gs.info("depts are " + typeof myDept+ "users dept are :" +myDept);
for (var i = 0; i < myDept.length; i++) {
var deptGr = new GlideRecord('cmn_department');
deptGr.addQuery('u_active', true);
deptGr.addQuery('sys_id', myDept[i]);
deptGr.query();
if (deptGr.next()) {
var activeDept = [];
activeDept.push(deptGr);
gs.info(activeDept);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2023 10:09 PM
your requirement is not clear.
Also where are you using this script?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2023 04:53 AM
I am using this script in script include. I tried it as an background script. I didn't get any answer.