How to auto-populate Department and Manager?

Matt Nolan1
Kilo Contributor

Hi,

I am attempting to create a variable set that automatically fills in the following information for use on catalog items and record producers. (variable name in parentheses) 

User who opened the request (requesting_user)

User's department (users_department)

User's manager (users_manager)

 

The requesting user fills in because I have the default value set to javascript:gs.getUserID()

Below are the OnLoad and OnChange scripts i have written

function onLoad(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.getReference('requesting_user', setFields);
}

function setFields(user) {
g_form.setValue('users_manager', user.manager);
g_form.setValue('users_department', user.department);
}

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.getReference('requesting_user', setFields);
}

function setFields(user) {
g_form.setValue('users_manager', user.manager);
g_form.setValue('users_department', user.department);
}

 

What am I doing wrong here? 

 

Thank you in advance for your responses

1 ACCEPTED SOLUTION

Resolved the issue. Please try now 🙂

Here are the modification

1. Set client script UI type to all. This is required to trigger script in Service Portal

2. Update OnChange CS to include variable name.

3. Updated Department variable to point to department table and not sys_user.

 

I hope this helps.

 

- Pradeep Sharma

View solution in original post

9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Matt,

Replace g_form.getReference('requesting_user', setFields); with var user = g_form.getReference('requesting_user', setFields);

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getReference('requesting_user', setFields);
}

function setFields(user) {
g_form.setValue('users_manager', user.manager);
g_form.setValue('users_department', user.department);
}

 

- Pradeep Sharma

Hi  Pardeep Sharma,

 

It is still not showing up. I adjusted both the onLoad and onChange to add the var user =.

I also attempted to do so while impersonating a user and it had the same behavior.

 

Thank you

 

Hi Matt,

 

Could you please reproduce this on the PDI instance and share the instance URL with me?

 

- Pradeep Sharma