- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 06:12 PM
I am attempting to use an onChange Client Script to populate the C# (employee_number) field on a form when the student_name field changes. Both student_name and employee_number are reference fields that point to the sys_user table from the form, while employee_number on the sys_user table is a string. I keep getting a no student found with sys_id error when the student_name field changes on the form. However, I can verify the sys_id returns the correct user when I query the sys_user table. I have also confirmed the employee_number for said student is not empty. So, I am not sure why it's showing as invalid on the form.
function onChange(control, oldValue, newValue, isLoading) {
// Skip if the form is still loading or if no student is selected
if (isLoading || newValue == '') {
return;
}
// Ensure the value is a valid Sys ID
if (!newValue || typeof newValue !== 'string') {
g_form.addErrorMessage('No student selected or invalid value.');
return;
}
// Log the value passed to check if it's a valid Sys ID
console.log('newValue from student_name field (Sys ID): ' + newValue);
// Create a GlideRecord object to query the sys_user table for the selected student
var userGR = new GlideRecord('sys_user');
// Query sys_user table by the Sys ID of the student (newValue is the Sys ID from student_name)
if (userGR.get(newValue)) { // Use the Sys ID directly
var employeeSysId = userGR.sys_id; // Sys ID of the user from sys_user table
// Check if employeeSysId exists (this will always be true if the Sys ID is valid)
if (employeeSysId) {
// Set the employee_number field with the Sys ID from sys_user
g_form.setValue('employee_number', employeeSysId);
console.log('Employee Sys ID found: ' + employeeSysId);
} else {
// Clear the field if no employee found (this is mostly redundant, as employeeSysId should always exist)
g_form.setValue('employee_number', '');
console.log('No employee found for this student.');
}
} else {
// Log the failure to get the record from sys_user
console.log('Error: No student found for Sys ID: ' + newValue);
g_form.addErrorMessage('No student found for the selected Sys ID: ' + newValue);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:54 PM
Ideally this is how you should do the setup, no point in having 2 variables referring same table
1st variable reference to sys_user
2nd variable should be of type Single line text
Based on selection of 1st populate the 2nd one
For this you can use auto populate feature and no scripting is required
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-28-2025 06:38 PM
@appstorm First thing first, it is not recommended to make GlideRecord queries directly from a client script as they block the UI thread and don't work on the Service Portal.
You can use GlideAjax + Script include to implement this. For more information please refer to https://servicenow.org.in/servicenow-tutotrial/servicenow-glideajax/
Also, since I am not sure about the field types on your form. You can try the following script to see if it works.
function onChange(control, oldValue, newValue, isLoading) {
// Skip if the form is still loading or if no student is selected
if (isLoading || newValue == '') {
return;
}
// Ensure the value is a valid Sys ID
if (!newValue || typeof newValue !== 'string') {
g_form.addErrorMessage('No student selected or invalid value.');
return;
}
// Log the value passed to check if it's a valid Sys ID
console.log('newValue from student_name field (Sys ID): ' + newValue);
// Create a GlideRecord object to query the sys_user table for the selected student
var userGR = new GlideRecord('sys_user');
// Query sys_user table by the Sys ID of the student (newValue is the Sys ID from student_name)
if (userGR.get('name',newValue)) { // Use the Sys ID directly
var employeeSysId = userGR.sys_id; // Sys ID of the user from sys_user table
// Check if employeeSysId exists (this will always be true if the Sys ID is valid)
if (employeeSysId) {
// Set the employee_number field with the Sys ID from sys_user
g_form.setValue('employee_number', employeeSysId);
console.log('Employee Sys ID found: ' + employeeSysId);
} else {
// Clear the field if no employee found (this is mostly redundant, as employeeSysId should always exist)
g_form.setValue('employee_number', '');
console.log('No employee found for this student.');
}
} else {
// Log the failure to get the record from sys_user
console.log('Error: No student found for Sys ID: ' + newValue);
g_form.addErrorMessage('No student found for the selected Sys ID: ' + newValue);
}
}
If this doesn't work then try posting the snapshot of your form and details of student_name and employee_number field on the sys_user table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 06:39 PM
Hi @appstorm
Refer the below link.
https://www.servicenow.com/community/developer-forum/onchange-client-script/m-p/2351886
Thanks
dgarad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 07:54 PM
Ideally this is how you should do the setup, no point in having 2 variables referring same table
1st variable reference to sys_user
2nd variable should be of type Single line text
Based on selection of 1st populate the 2nd one
For this you can use auto populate feature and no scripting is required
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-29-2025 06:47 AM
Can't believe I missed this - made it so much easier!