Skip to Content

GlideAjax Server Call

Call server-side Script Include from client-side to fetch data or perform operations without page refresh.

onChange Table: incident Field: caller_id
#glideajax #ajax #server-call #async #onChange

Script Code

JavaScript
1function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2  // Exit if form is loading or value is empty
3  if (isLoading || newValue === '') {
4    return;
5  }
6
7  // Configuration: Update these values for your needs
8  var scriptIncludeName = 'UserInfoUtils';     // Name of your Script Include
9  var functionName = 'getUserOpenTickets';     // Function in the Script Include
10  var targetField = 'u_open_tickets_count';    // Field to populate with result
11
12  // Create GlideAjax object
13  var ga = new GlideAjax(scriptIncludeName);
14
15  // Set the function to call (maps to Script Include method)
16  ga.addParam('sysparm_name', functionName);
17
18  // Add parameters to send to server
19  ga.addParam('sysparm_user_id', newValue);
20
21  // Optional: Add more parameters as needed
22  // ga.addParam('sysparm_table', 'incident');
23  // ga.addParam('sysparm_active', 'true');
24
25  // Make the asynchronous call
26  ga.getXMLAnswer(function(response) {
27    // This callback function runs when server responds
28
29    // Handle the response
30    if (response) {
31      // Set the target field with the response
32      g_form.setValue(targetField, response);
33
34      // Optional: Show info message
35      g_form.addInfoMessage('User has ' + response + ' open tickets');
36
37    } else {
38      // Handle empty or error response
39      g_form.setValue(targetField, '0');
40      gs.addErrorMessage('Unable to retrieve user information');
41    }
42  });
43
44  // Optional: Show loading message
45  g_form.addInfoMessage('Loading user information...');
46}

How to Use

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

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts