The error you're encountering, TypeError: $ is not a function, typically suggests that jQuery ($) is not available or properly loaded in your ServiceNow environment at the time your onChange client script is executed. This situation often arises more commonly in scoped applications where certain libraries and functions might not be automatically available due to the scope's restrictions or differences in the loading order.
Here are some steps and solutions to troubleshoot and resolve this issue:
1. Ensure jQuery is Loaded
Before using jQuery ($), ensure that the jQuery library is loaded. Since ServiceNow comes with built-in jQuery, you can do this by running a simple check prior to your script execution. Use this snippet at the beginning of your script:
if (typeof jQuery !== 'undefined') {
// jQuery is loaded
} else {
// jQuery is not loaded
console.error('jQuery is not loaded or not accessible.');
}
2. Use jQuery Alias
In scoped applications, sometimes the $ alias for jQuery might not be available or could be overwritten by another library. Instead, use the jQuery keyword directly:
jQuery(document).ready(function() {
// Your code here
});
3. Load jQuery Manually
If jQuery is not available, you may need to load it manually within your script. Note that this is an uncommon scenario, as ServiceNow should have jQuery available, but you can use the following approach to dynamically load it:
if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = function() {
// Your code that depends on jQuery here
jQuery(document).ready(function() {
// Your code here
});
}
} else {
jQuery(document).ready(function() {
// Your code here
});
}
4. ServiceNow Glide APIs
Consider using ServiceNow built-in client-side APIs for many operations instead of jQuery. The g_form and g_user APIs are very powerful and can replace many use cases where you might traditionally use jQuery. For example:
- Accessing form data:
g_form.getValue('field_name')
- Setting field values:
g_form.setValue('field_name', 'value')
Example Adjusted Client Script Using g_form:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Use ServiceNow's g_form API
var myField = g_form.getValue('my_field');
if (myField) {
g_form.addInfoMessage('Field value: ' + myField);
}
}
5. Check Scoped Application Settings
Ensure your scoped application allows usage of certain libraries if they are intended to be utilized across the board. Scoped applications might have specific restrictions which need to be addressed via configuration or by requesting appropriate permissions.
Summary
To fix the TypeError: $ is not a function error in your onChange client script in a scoped application in ServiceNow, verify that jQuery is loaded and accessible, use the jQuery alias instead of $, utilize ServiceNow's g_form API for common form-related operations, and ensure correct configurations in scoped applications to avoid such issues.
By following these suggestions, you should be able to resolve the issue and ensure that your onChange client script functions correctly in both global and scoped applications within ServiceNow.