- 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 09:02 PM
this script not working when on load? because i get requestor from login user. and the script running in on load condition. g_form.setValue('u_requestor', g_user.userID, g_user.getFullName()); --> this script and type is onLoad.
i'm sorry because i ask too much...

- 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 10:24 PM
thank you. this is so helpful. the script all working.
thank you so much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2017 08:20 PM
i try to script g_form.setValue('u_department', g_user.getDepartmentID()); its work when i opened it via catalog in my instance,but its not work when i try to open it via service portal.