- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2025 12:26 PM
Hi Developers,
I have created 2 fields in a Catalog i.e. Employee Name (requested_for_employee_name) and Employee ID (requested_for_employee_id). Employee Name is a Reference field to the User (sys_user) table.
I have written the below Catalog Client Script on Employee Name field to set the Employee ID field with the Employee Number from User Table.
The issue is Employee ID field is not setting up with the employee number when value in the Employee Name field changes. Please suggest.
Catalog Client Script: -
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2025 07:43 PM
you can use getReference with callback rather than GlideRecord
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('requested_for_employee_id');
}
var ref = g_form.getReference('requested_for_employee_name', callBackMethod);
}
function callBackMethod(ref) {
if (ref.employee_number)
g_form.setValue('requested_for_employee_id', ref.employee_number);
}
OR
you can do this without scripting with auto populate feature
Auto-populate a variable based on a reference type variable (Utah)
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2025 12:43 PM
You would use EmpSysId in the addQuery, not UserSysId. GlideRecords are not supported or recommended in Client Scripts. You should instead be in the habit of using getReference
or GlideAjax to call a Script Include
both excellent tools to have in your belt. In this case, you can also use the variable Auto-populate feature on the definition of the Employee ID to lookup the field from the Employee Name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 12:45 AM
I was not using getReference as it is not good from Performance point of view.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2025 01:22 PM
Hello @NehaSNOW
Few things to note:
- Make sure the employee number field has value.
- Glide record cannot be used in client script as this API supports server side scripting.
- Since both the variables are populated from same table, this can be achieved using dot walking and no scripting is required.
Refer the below screenshot to auto-populate the Employee ID variable.
The dependent question: Employee Name
Dot walk to Employee number field.
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 12:57 AM
Hello @NehaSNOW
Thank you for marking my response as helpful.
As per the new community guidelines you can mark multiple response as correct answers.
If my response helped you, kindly mark it as an accepted solution.
It helps future readers to locate the solution easily in community.
Thank You
Juhi Poddar