- 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 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 09:28 PM
The issue lies in your Catalog Client Script in two areas: the way the sys_id is passed to the GlideRecord query and the logic for fetching and setting the employee number. GlideRecord cannot be used directly in a Catalog Client Script because it runs on the server-side, while the script executes on the client-side.
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the server-side script include to fetch employee_number
var ga = new GlideAjax('FetchEmployeeDetails');
ga.addParam('sysparm_name', 'getEmployeeNumber');
ga.addParam('sysparm_user_sys_id', newValue); // Pass the sys_id of the selected user
ga.getXMLAnswer(function(response) {
var employeeNumber = response;
g_form.setValue('requested_for_employee_id', employeeNumber); // Set the value
});
}
Script Include:
var FetchEmployeeDetails = Class.create();
FetchEmployeeDetails.prototype = {
initialize: function() {},
getEmployeeNumber: function() {
var userSysId = this.getParameter('sysparm_user_sys_id'); // Get sys_id passed from the client script
var employeeNumber = '';
// Query the sys_user table for the employee number
var userRecord = new GlideRecord('sys_user');
if (userRecord.get(userSysId)) {
employeeNumber = userRecord.employee_number + ''; // Convert to string
}
return employeeNumber; // Return the employee number
},
type: 'FetchEmployeeDetails'
};