How to auto populate Department from the User [sys_user} table

carlavanskaik
Giga Contributor

This is the first custom application attempted. 

I would like to auto populate the value from the User [sys_user] department field, but I'm not quite sure how to do that. (Refer to attached screen print)  I was able to successfully auto populate the Requestor field, but am having trouble with the Department field.

Perhaps the missing link for me is that I must somehow associate the Department field value with the Requestor's User ID. Unfortunately, I'm not sure how to do that.

So, I appreciate any help which can be provided!

1 ACCEPTED SOLUTION

Taking the Incident form lay-out as example. You could add the field doltwalked like:

find_real_file.png

find_real_file.png

If I now select a User which has a Department, like on my PDI "HR" is my department, automatically the Department is shown:

find_real_file.png 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

16 REPLIES 16

Pratiksha Kalam
Kilo Sage

Hello,

you have to write script include and client script for this. Department field from sys_user refers to"cmn_department" table.

Below link will help you,

https://community.servicenow.com/community?id=community_question&sys_id=1ca5cbeddbd8dbc01dcaf3231f96...

Thanks,

Pratiksha

 

 

Jaspal Singh
Mega Patron
Mega Patron

Hi Carl,

 

You can pass javascript: gs.getUser().getRecord().getValue('department');

as default reference qualifiere for Department field. 

This would work well always in case where the Requestor is logged in user. If the Requestor field is chagned to some other user than logged in user then you need to have a onChange() client script in place that will work with combination of GlideAjax in client script & a script include (recommended).

Also, you could try below as onChange() client script that runs onchange of Requestor field as below.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var getnameis=g_form.getReference('requestor_variable',getuserdetails);//replace requestor_variable with variable name for requestor
	
	function getuserdetails(getnameis)
	{
		g_form.setValue('department',getnameis.department); //replace department with department variable name
	}

   //Type appropriate comment here, and begin script below
   
}

 

Be aware that Department is a reference field looking at the example. So only providing the sys_id or name, causes an additional call to be made (= performance loss).

find_real_file.png

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

I tried the following javascript as you suggested; however, no value populates in the field.  I had envisioned the value auto populating based on the user who is signed in and submitting the request.  Should I have expected the value to populate on load?    

You can pass javascript: gs.getUser().getRecord().getValue('department');

as default reference qualifiere for Department field. 

Hi,

Are you using this in custom scope application?

Regards
Ankur

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