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

Dhananjay Pawar
Kilo Sage

Hi, Based on logged in user you want to populate users department.

If yes then create onLoad client script as below,

 

function onLoad() {

var ga = new GlideAjax('ScriptIncludeDemo');
ga.addParam('sysparm_name','GetDetails');
ga.getXML(callback);
function callback(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");


g_form.setValue('deparment',answer);

}
}

 

write script include

SI name - ScriptIncludeDemo

client callable - checked

 

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

GetDetails : function()
{
var user = gs.getUserID();
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',user);
gr.query();

while(gr.next())
{
return gr.department.name;

}
},


type: 'ScriptIncludeDemo'
});

 

 

Mark correct/helpful based on impact.

Thanks,

Dhananjay.

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

Yeah thats correct.

Thanks Mark.

MrMuhammad
Giga Sage

Hi,

Can you try this in the default value of Department field .

javascript:gs.getUser().getDepartmentID();

 

Please mark this accepted/helpful, if applicable.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Sharjeel, this does not seem to populate the Department field with the value from the User [sys_user] table.  However, I appreciate the recommendation.