- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 07:31 PM
Hello.. i'm a newbie in service now. i want to ask something about record producers client script. how to fill department field on automatically depend on requestor. i use g_user to get requestor login in script. and how to fill department and approver depend on user field when i choose user. i have been search in community and wiki. but i find how to automatically fill it use default value. Please help.
Thank you.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 09:56 PM
Manger field need to be referenced to sys_user table bacause if you make cmn_department table reference then it will display the department name there always as per the display flag of the field in cmn_department table. Manager also comes from sys_user table so it should be referenced to that table.
onLoad() client script should work, you can have two client script.
1. To load the department name with onLoad() client script.
function onLoad() {
//Type appropriate comment here, and begin script below
g_form.setValue('u_requestor', g_user.userID);
var caller = g_form.getReference('u_requestor', doAlert);
}
function doAlert(caller) {
g_form.setValue('u_department', caller.department);
}
2. an onChange() client script on department field when value being set then trigger the onChange() Client script to get the manager name of the department.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var department = g_form.getReference('u_department', doAlert);
}
function doAlert(department) {
g_form.setValue('caller_id', department.dept_head);
}
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 07:47 PM
Hi Zulhiyati,
Please use getReference() function to get the details of the a reference field value, Please check below link for more information.
GlideForm (g form) - ServiceNow Wiki
function onChange(control, oldValue, newValue, isLoading) {
var caller = g_form.getReference('caller_id', doAlert);
}
function doAlert(caller) {
g_form.setValue('FIELD NAME OF DEPARTMENT', caller.department);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 08:15 PM
thank you for response to my question.
im sorry,but dont understand how to use getReference. i try it, but it not work, i dont know how to link it. my department of requestor field is u_department. that field was reference to department (cmn_department table). requestor reference to sys_user. this my script..
g_form.setValue('u_requestor', g_user.userID, g_user.getFullName());
g_form.setMandatory('u_requestor', true);
g_form.setMandatory('u_application_name', true);
g_form.setMandatory('u_time_period', true);
g_form.setMandatory('u_user', true);
g_form.setMandatory('description', true);
g_form.setReadOnly('u_department', true);
g_form.setReadOnly('u_role', true);
g_form.setReadOnly('u_department_user', true);
g_form.setReadOnly('u_approver', true);
g_form.setReadOnly('u_license_availability', true);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 08:27 PM
Hello,
If i understand correctly then Department (u_department) is a reference field to cmn_department table and User is a reference field to sys_user table, now you want to set the department value based upon the User value? Please try to create onChange() client script on User field
function onChange(control, oldValue, newValue, isLoading) {
var caller = g_form.getReference('u_user', doAlert); //Hoping u_user is a backend field name or User field
}
function doAlert(caller) {
g_form.setValue('u_department', caller.department);
}
who should be approver?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 08:47 PM
approver is manager of department. so i make this field reference to table cmn_department too... i confused where to link it. so when i saw in manager in department i reference to it.