We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to find active departments that users are member of?

Sathiskumar_D
Giga Sage

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

 

1 ACCEPTED SOLUTION

Sathiskumar_D
Giga Sage

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();

 

 

View solution in original post

10 REPLIES 10

Tony Chatfield1
Kilo Patron

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();

Omkar Mone
Mega Sage

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);
        }
}

Ankur Bawiskar
Tera Patron

@Sathiskumar_D 

your requirement is not clear.

Also where are you using this script?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

I am using this script in script include. I tried it as an background script. I didn't get any answer.