How to get value from reference table on client script record produces

zulhiyatiuwi
Tera Expert

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.

1 ACCEPTED SOLUTION

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.


View solution in original post

8 REPLIES 8

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...


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.


thank you. this is so helpful. the script all working.


thank you so much.


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.