Can a OnChange Client script works on Multiple Variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2024 11:23 PM
I have a Task Regarding Catalog item. A OnChange() Client Script has to be written. I had written all the code correctly functionality also working as usually. Here is my Code:
Here For this script i am taking 'Variable name' as None, because i need this script to run every time whenever a user a value in the form. But this was not working like that, if we take variable name as none , the script was not running also. Is there Any solution for this..??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2024 11:48 PM
Hello @Aditya02,
In ServiceNow, when you set the variable name to "None" for a client script, it essentially means the script won't be directly associated with a specific variable change. This can lead to the script not being triggered as expected when other variables are changed.
Instead of an onChange client script, consider using an onLoad client script or a Global client script. In the global script, you can set up listeners for changes on specific fields:
function onLoad() {
// Attach the onChange functionality to specific fields
var fieldsToWatch = ['contact_type', 'state', 'impact', 'urgency', 'priority', 'subcategory', 'assigned_to'];
fieldsToWatch.forEach(function (field) {
var element = g_form.getControl(field);
if (element) {
element.onchange = function () {
alert("Element ID: " + element.id + ", New Value: " + element.value);
onChange(element, null, element.value, false);
};
}
});
}
Using the global script method is generally more efficient if you have multiple fields to monitor. It reduces the need for multiple client scripts and makes it easier to manage.
Thanks & Regards
Siddhesh Jadhav
If this solution helps, please mark it as helpful and accepted.