Debounced Search with GlideAjax
Implement a debounced search to avoid excessive server calls when users type in a search field, improving performance.
onChange Table: incident Field: u_search_field
#onChange #search #debounce #performance #glideajax #optimization
Script Code
JavaScript
1// Global variable to store timeout (outside function)
2var searchTimeout;
3
4function onChange(control, oldValue, newValue, isLoading, isTemplate) {
5 // Exit if form is loading
6 if (isLoading) {
7 return;
8 }
9
10 // Configuration
11 var debounceDelay = 500; // Wait 500ms after user stops typing
12 var minSearchLength = 3; // Minimum characters before searching
13 var resultsField = 'u_search_results'; // Field to display results
14
15 // Clear any existing timeout
16 if (searchTimeout) {
17 clearTimeout(searchTimeout);
18 }
19
20 // Clear results if search is too short
21 if (!newValue || newValue.length < minSearchLength) {
22 g_form.clearValue(resultsField);
23 g_form.hideFieldMsg(resultsField);
24 return;
25 }
26
27 // Show searching indicator
28 g_form.showFieldMsg(resultsField, 'Searching...', 'info', false);
29
30 // Set new timeout to execute search after delay
31 searchTimeout = setTimeout(function() {
32 // Make GlideAjax call to search
33 var ga = new GlideAjax('SearchUtils'); // Create corresponding Script Include
34 ga.addParam('sysparm_name', 'performSearch');
35 ga.addParam('sysparm_search_term', newValue);
36 ga.addParam('sysparm_search_table', 'cmdb_ci'); // Table to search
37 ga.addParam('sysparm_search_fields', 'name,serial_number'); // Fields to search
38
39 ga.getXMLAnswer(function(response) {
40 if (response) {
41 try {
42 var results = JSON.parse(response);
43
44 // Clear previous message
45 g_form.hideFieldMsg(resultsField);
46
47 if (results.count > 0) {
48 // Display results count
49 var message = 'Found ' + results.count + ' matching record(s)';
50 g_form.showFieldMsg(resultsField, message, 'info');
51
52 // Optionally populate a reference field with first result
53 if (results.records && results.records.length > 0) {
54 var firstResult = results.records[0];
55 // g_form.setValue('u_related_ci', firstResult.sys_id);
56 }
57
58 // Store results in hidden field or display in custom way
59 g_form.setValue(resultsField, JSON.stringify(results.records));
60 } else {
61 g_form.showFieldMsg(resultsField, 'No matching records found', 'warning');
62 g_form.clearValue(resultsField);
63 }
64 } catch (e) {
65 g_form.showFieldMsg(resultsField, 'Error parsing search results', 'error');
66 console.error('Search error:', e);
67 }
68 } else {
69 g_form.showFieldMsg(resultsField, 'Search failed - no response', 'error');
70 }
71 });
72 }, debounceDelay);
73}
How to Use
1. Create an onChange Client Script on your search field
2. Create a Script Include named 'SearchUtils' with 'performSearch' function
3. Adjust debounceDelay and minSearchLength for your needs
4. Customize the search table and fields
5. Update resultsField to match your form
6. Test by typing quickly - should only trigger after pause
7. Monitor server logs to confirm reduced call frequency
Related Scripts
Auto-populate Related Fields
Automatically populate fields when a reference field changes (e.g., populate caller's phone and email when caller is selected).
Conditional Mandatory Fields
Make fields mandatory based on the value of another field (e.g., require close notes only when state is Resolved).
Dynamic Field Visibility
Show or hide fields based on another field's value (e.g., show "Other reason" field only when user selects "Other" from dropdown).