logo

NJP

Calling a catalog item variable onChange function programmatically

Import · Jun 04, 2018 · article

I have written an onChange function for a catalog item variable. I was told that we need another control, Y, that will have the same behavior as the first control, X. I would normally move the code to a new function and put it after an onLoad function and then call the new function from the onChange functions. The onLoad script would look like: function onLoad() { } function myNewFunction(newValue) { }The onChange script for control X would look like: function onChange (control, oldValue, newValue, isLoading) { myNewFunction(newValue); }The onChange script for control Y would look like: function onChange (control, oldValue, newValue, isLoading) { myNewFunction( g_form.getValue('X') );

}

I didn't want to mess with what to call myNewFunction and passing the argument from the onChange function to the new function but instead I just wanted to call the onChange function for control X from the onChange function for control Y. I looked at the code for the onChange function and saw that it was set by a function called variableOnChange. The argument for the variableOnChange function was the name of the control that started with "IO:" followed by the control sys_id. So in the onChange function for control Y I put the following code: var X = g_form.getControl('X');

if(X && X.name) variableOnChange( X.name );

So now when the onChange function for control Y is called it also calls the onChange function for control X. Since I am using the OOB function variableOnChange, the arguments for the onChange function for control X come from control X.

Labels:

image

View original source

https://www.servicenow.com/community/developer-articles/calling-a-catalog-item-variable-onchange-function/ta-p/2329919