Would you be interested in adding this to your site?
function onChange(control,oldValue,newValue,isLoading){
if(!isLoading) {
var newValueString = String(newValue);
switch(newValueString ) {
case 'Option One':
g_form.setDisplay('existing_widget',true);
g_form.setDisplay('existing_thingy',true);
break;
case 'Option Two':
g_form.setDisplay('existing_widget',false);
g_form.setDisplay('existing_thingy',false);
break;
default:
g_form.setDisplay('existing_widget',false);
g_form.setDisplay('existing_thingy',false);
break;
}
}
}
if(!isLoading) {
var newValueString = String(newValue);
switch(newValueString ) {
case 'Option One':
g_form.setDisplay('existing_widget',true);
g_form.setDisplay('existing_thingy',true);
break;
case 'Option Two':
g_form.setDisplay('existing_widget',false);
g_form.setDisplay('existing_thingy',false);
break;
default:
g_form.setDisplay('existing_widget',false);
g_form.setDisplay('existing_thingy',false);
break;
}
}
}
It’s a catalogue client script that shows/hides form inputs based on the
option that is chosen. The tricky thing, and the thing that stumped us for
quite a while, is that the system variables provided by the OnChange
function are objects and not strings. So the String(newValue) is critical
or the case statement will fail.To summarize: OnChange Catalog Client Scripts work differently than regular OnChange client scripts. In order to use the ‘newValue’ and ‘oldValue’ parameters for comparison purposes they need to be converted into a different variable type. The easiest way to do this is to use ‘String(newValue)’ or ‘newValue.toString()’.
Somewhat along these lines is a “feature” I discovered with pushing sys_ids into an array while looping through a GlideRecord. Here’s what I had at first:
var gr = new GlideRecord("incident");
gr.query();
while(gr.next()) {
ids.push(gr.sys_id);
}
When I was done, every value in the array was the same. They were all equal to the sys_id of the last Glide Record that was found. This is because “gr.sys_id” is an object, so what you end up with is a bunch of pointers in your array all pointing to gr.sys_id, which contains that last sys_id.
I solved the problem by changing my code to this:
This should work too:
You might need a “new” before “String”.
I hope that helps someone out there!
Thanks Peter,
I remember this giving me fits when I first came across it. Good tip!