How to get the ID of a department from a user

mi2022
Tera Contributor

Hi,

I want to get the ID of a department from a user.

I'm doing a dot walk with getReference, but I can't get it.

*Catalog client scripts

How do I get it?

Regards,

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, as a client-side requirement, I would look at GlideAjax to lookup\return the information you require

GlideAjax | ServiceNow Developers

Mohit_Gupta
Tera Guru
Tera Guru

Hi @mi2022 ,

 

You'll not get department name by getting reference from sys_user table. You'll only get the sys_id of department as that's what is actually stored at user level. Dot walking works in business rules but not at client script level. So either you'll have to do two getReference (one for user and second for department) or you can write a GlideAjax as you've mentioned in your post.

 

I hope this helps 

Please mark my answer correct if this helps you 

Thanks

Mohit

jaheerhattiwale
Mega Sage
Mega Sage

@mi2022 There is department field on users table

jaheerhattiwale_0-1670468243358.png

 

Script in catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

g_form.getReference('<USER REFERENCING FIELD HERE>', getDepartment); // getDepartment is our callback function

}

function getDepartment(user) { // reference is passed into callback as first arguments
alert('Users department is :'+user.getValue('department'));
}

 

Assuming you are using on change client script.

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023