Get department ID of user

Arka Banerjee
Kilo Guru

Hi All,

 

I have to fetch the department ID of a user in my catalog, now I have a script include which fetches other user details like phone number, email, etc and populates it on the form. I want to get the department ID of that user which is actually contained in department table. Kindly help me fetch the same.

1 ACCEPTED SOLUTION

Hi,

That is odd because the dot walk should work.

Could you try this version of the script?

var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getEmployeeDetails: function () {
    var userName = this.getParameter('sysparm_user');
    var user = new GlideRecord('sys_user');
    var result = {
      //Create an object to store the User data
      phoneNumber: '',
      email: '',
      location: '',
      manager: '',
      department: '',
    };

    if (user.get(userName)) {
      var departmentRec = new GlideRecord('cmn_department');
      departmentRec.addQuery('name', user.department.getDisplayValue().toString());
      departmentRec.query();
      if (departmentRec.next()) {
        result.department = departmentRec.id.toString();
      }

      result.email = user.email.toString();
      result.phoneNumber = user.mobile_phone.toString();
      result.location = user.location.getDisplayValue();
      result.manager = user.manager.getDisplayValue();
    }
    return JSON.stringify(result);
  },

  type: 'userDetailsUtil',
});

Hope this helps

View solution in original post

3 REPLIES 3

Dan H
Tera Guru

Hi,

Share your script include and I will add the changes to it.

 

Hope this helps

Hi Dan,

 

PFB.

 

var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getEmployeeDetails: function() {
var userName = this.getParameter('sysparm_user');
var user = new GlideRecord('sys_user');
var result = { //Create an object to store the User data
phoneNumber: "",
email: "",
location: "",
manager: "",
department: ""
};

if (user.get(userName)) {
result.email = user.email.toString();
result.phoneNumber = user.mobile_phone.toString();
result.location = user.location.getDisplayValue();
result.manager = user.manager.getDisplayValue();
result.department = user.department.id.getDisplayValue();
}
return JSON.stringify(result);
},

type: 'userDetailsUtil'
});

 

If I just do user.department.getDisplayValue(), its giving me the Department name but to get the ID, the ID dotwalk is not working..

Hi,

That is odd because the dot walk should work.

Could you try this version of the script?

var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getEmployeeDetails: function () {
    var userName = this.getParameter('sysparm_user');
    var user = new GlideRecord('sys_user');
    var result = {
      //Create an object to store the User data
      phoneNumber: '',
      email: '',
      location: '',
      manager: '',
      department: '',
    };

    if (user.get(userName)) {
      var departmentRec = new GlideRecord('cmn_department');
      departmentRec.addQuery('name', user.department.getDisplayValue().toString());
      departmentRec.query();
      if (departmentRec.next()) {
        result.department = departmentRec.id.toString();
      }

      result.email = user.email.toString();
      result.phoneNumber = user.mobile_phone.toString();
      result.location = user.location.getDisplayValue();
      result.manager = user.manager.getDisplayValue();
    }
    return JSON.stringify(result);
  },

  type: 'userDetailsUtil',
});

Hope this helps