- 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 11:36 PM
Hello @appstorm ,
Hope you are doing well!
To achieve the above requirement you need to do it by using client script + script Include using glideAjax API.
Refer it: Script Include and Client Script using glideajax
If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.
Thanks,
Vaibhav Nikam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 06:17 AM - edited 01-29-2025 06:20 AM
Thanks for the reply! I added a SI to the revised Client Script, but am still getting an error "Unhandled exception in GlideAjax. Cannot read properties of null (reading 'responseXML')" even when hardcoding a sys_id in the script include.
CS:
function onChange(control, oldValue, newValue, isLoading) {
// Skip if the form is still loading or if no student is selected
if (isLoading || newValue == '') {
return;
}
// Log the selected Sys ID for debugging
console.log('Selected Sys ID from student_name field: ' + newValue);
// Initialize GlideAjax request to call the Script Include
var ga = new GlideAjax('GetStudentEmployeeNumber');
ga.addParam('sys_id', newValue); // Pass the Sys ID of the student
// Make the GlideAjax request and handle the response
ga.getXMLAnswer(function(response) {
try {
// Parse the response as JSON
var jsonResponse = JSON.parse(response);
// Check for error in response
if (jsonResponse.error) {
console.log('Error from server: ' + jsonResponse.error);
g_form.addErrorMessage('Error: ' + jsonResponse.error); // Display error message to user
} else {
// Extract employee_number from the JSON response
var employeeNumber = jsonResponse.employee_number;
console.log('Employee Number from JSON: ' + employeeNumber);
g_form.setValue('employee_number', employeeNumber); // Update the field with the employee number
}
} catch (e) {
console.log('Error processing response: ', e); // Log any parsing errors
g_form.addErrorMessage('Error processing response.');
}
});
}
SI
var GetStudentEmployeeNumber = Class.create();
GetStudentEmployeeNumber.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// This function is the client callable method
getEmployeeNumber: function() {
var sysId = this.getParameter('sys_id'); // Get sys_id from GlideAjax request
var userRecord = new GlideRecord('sys_user'); // Query the sys_user table
if (userRecord.get(sysId)) {
// Return employee number as a JSON response
var employeeNumber = userRecord.getValue('employee_number');
return new JSON().encode({
employee_number: employeeNumber
});
} else {
// If the user record isn't found, return an error in JSON format
return new JSON().encode({
error: 'User not found'
});
}
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 06:23 PM
Change to code as below.
CS:
ga.addParam('sysparm_sys_id', newValue);
SI:
var sysId = this.getParameter('sysparm_sys_id');
Thanks
dgarad