Can a OnChange Client script works on Multiple Variables

Aditya02
Tera Guru

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:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    // Delay execution to ensure UI policies are applied first
    setTimeout(function() {
        updateVars();
    }, 100); // Adjust delay time as needed
}

function updateVars() {
    var radioValue = g_form.getValue('variables'); // Ensure this matches your radio button variable name

    // Define checkboxes based on the radio button selected
    var checkboxes = [];
    if (radioValue === 'v1') {
        checkboxes = ['choice1', 'choice2', 'choice3'];
    } else if (radioValue === 'v2') {
        checkboxes = ['choice4', 'choice5', 'choice6'];
    } else if (radioValue === 'v3') {
        checkboxes = ['choice7'];
    }

    // Collect selected checkboxes
    var selectedCheckboxes = checkboxes.filter(function(checkbox) {
        return g_form.getValue(checkbox) === 'true';
    });
    alert(checkboxes+' '+selectedCheckboxes);
    // Update variable_1, variable_2, and variable_3 based on the selected checkboxes
    var variableNames = ['variable_1', 'variable_2', 'variable_3'];
    for (var i = 0; i < variableNames.length; i++) {
        g_form.setValue(variableNames[i], selectedCheckboxes[i] || '');
    }
}

// Attach onChange event to the radio button and checkboxes
var fieldsToWatch = ['variables', 'choice1', 'choice2', 'choice3', 'choice4', 'choice5', 'choice6', 'choice7'];
fieldsToWatch.forEach(function(field) {
    var element = g_form.getControl(field);
    if (element) {
        element.onchange = function() {
            onChange(element, null, element.value, false);
        };
    }
});

 

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..??

1 REPLY 1

Siddhesh Jadhav
Kilo Sage

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.