Client Scripts
GlideAjax Server Call
Call server-side Script Include from client-side to fetch data or perform operations without page refresh.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Exit if form is loading or value is empty
if (isLoading || newValue === '') {
return;
}
// Configuration: Update these values for your needs
var scriptIncludeName = 'UserInfoUtils'; // Name of your Script Include
var functionName = 'getUserOpenTickets'; // Function in the Script Include
var targetField = 'u_open_tickets_count'; // Field to populate with result
// Create GlideAjax object
var ga = new GlideAjax(scriptIncludeName);
// Set the function to call (maps to Script Include method)
ga.addParam('sysparm_name', functionName);
// Add parameters to send to server
ga.addParam('sysparm_user_id', newValue);
// Optional: Add more parameters as needed
// ga.addParam('sysparm_table', 'incident');
// ga.addParam('sysparm_active', 'true');
// Make the asynchronous call
ga.getXMLAnswer(function(response) {
// This callback function runs when server responds
// Handle the response
if (response) {
// Set the target field with the response
g_form.setValue(targetField, response);
// Optional: Show info message
g_form.addInfoMessage('User has ' + response + ' open tickets');
} else {
// Handle empty or error response
g_form.setValue(targetField, '0');
gs.addErrorMessage('Unable to retrieve user information');
}
});
// Optional: Show loading message
g_form.addInfoMessage('Loading user information...');
}How to use it
1. Create an onChange Client Script on your table 2. Create a corresponding Script Include (see Script Includes section) 3. Update the configuration variables for your use case 4. Test with various inputs to ensure proper error handling
Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.