OnChange Catalog Client Script Nuance

My friend Peter from Down Under sent me a great tip the other day about an issue he had with an onChange catalog client script. It’s something that I wasn’t aware of which goes to show you that there’s always something new that you can learn :). Hopefully this tip keeps someone from having to bang their head against a wall for a few hours.
I’m always open to suggestions on how to improve the site and its content. If you have any ideas, questions, or suggestions for the site just use the ‘Ask The Guru‘ link to submit them. Thanks Peter!

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;
      }
   }
}

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()’.

Date Posted:

January 6, 2010

Share This:

2 Comments

  1. Peter Oneppo April 17, 2011 at 3:10 pm

    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 ids = new Array();
     
    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:

     
    ids.push(gr.sys_id.toString())
     
    

    This should work too:

     
    ids.push(String(gr.sys_id));  
     
    

    You might need a “new” before “String”.

    I hope that helps someone out there!

    • Mark Stanger April 18, 2011 at 12:10 am

      Thanks Peter,

      I remember this giving me fits when I first came across it. Good tip!

Comments are closed.

Categories

Tags

Loading

Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.