Regarding gs.getUser().getDepartmentID()

N_Y_1014
Tera Contributor

Thank you for your help.

 

I am working on a requirement that when an unauthorized user logs into the service portal and opens my_request, only the records of the department to which he/she belongs should be displayed.

 

We also need to determine if the department the user belongs to and the department registered in the record is a partial match.

 

I have tried to script an ACL for read, but am struggling to get the department name of the logged in user.

 

I can get the sys_id of the department with gs.getUser().getDepartmentID(), but I cannot bring the department name.

 

Please tell me how to get the name of the department from the logged in user's information.

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @N_Y_1014 

 

As you are getting the sys_id of department & if I'm not wrong then you can glide record on "cmn_department" table on server side code

and get the Department name :

/* Will get the sys_id of dept*/ 
var deptSysid = gs.getUser().getDepartmentID();

var deptName;

/*Query on Department table */
var grDept = new GlideRecord('cmn_department');
grDept.addQuery('sys_id', deptSysid);
grDept.query();

if(grDept.next()){
    deptName = grDept.name;
}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@N_Y_1014 

query cmn_department table with that sysId and get the department name

var gr = new GlideRecord("cmn_department");
		gr.addQuery("sys_id", gs.getUser().getDepartmentID());
		gr.query();
		if (gr.next()) {
			var deptName = gr.name;
		}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Vishal Birajdar
Giga Sage

Hi @N_Y_1014 

 

As you are getting the sys_id of department & if I'm not wrong then you can glide record on "cmn_department" table on server side code

and get the Department name :

/* Will get the sys_id of dept*/ 
var deptSysid = gs.getUser().getDepartmentID();

var deptName;

/*Query on Department table */
var grDept = new GlideRecord('cmn_department');
grDept.addQuery('sys_id', deptSysid);
grDept.query();

if(grDept.next()){
    deptName = grDept.name;
}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates