onChange client script based on Multiple field

Shiraz2
Mega Guru

I am using the following script which works fine as an onLoad client Script. However, I was wondering if it is possible to revise it to make it work as an onChange Client script

function onLoad() {

    //Type appropriate comment here, and begin script below

      g_form.setDisplay('u_test_case_count', false);

  var tcCount = g_form.getIntValue('u_test_case_count');

  var i = tcCount;

  for(i = 1; i <= tcCount; i++){

  var varName = g_form.getValue("u_"+i+"_pass_fail");

  if (varName == 'Fail'){

  g_form.setDisplay('u_add_new_act_result',false) ;

  }

  }

}

Little bit about what the code is.

I have about 15 Choice fields (u_X_pass_fail, where X= 1-15) that display on a form one after another. This is achieved by clicking a button called Add New(u_add_new_act_result) which also adds a counter to another field called TC Count(u_test_case_count) . Once the choice field appears and if the selection of 'Fail' is selected,   I want that Add New button to hide/disappear. I can achieve this using UI policy very easily, but client script would work easily.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Shiraz,



Short answer: You need an onChange client script for each field. There is no universal "onChange for all" concept when dealing with onChange client scripts. You specify which field to watch and when it changes, the script reacts to that changed field/value.



Docs: Client Scripts


Docs: GlideForm


Client Script Best Practices - ServiceNow Wiki      


View solution in original post

6 REPLIES 6

Chuck Tomasi
Tera Patron

Hi Shiraz,



Short answer: You need an onChange client script for each field. There is no universal "onChange for all" concept when dealing with onChange client scripts. You specify which field to watch and when it changes, the script reacts to that changed field/value.



Docs: Client Scripts


Docs: GlideForm


Client Script Best Practices - ServiceNow Wiki      


this is exactly how i have handled such scenarios.


You basically create onchange scripts on each of these fields, and on each script you check the valye on other ones and based on that just call a function, which is in your onload script. this way you will save duplication the whole logic in each script.


-Anurag

what if the 'field' you were watching was a MRVS variable set?  would that change the result any?  this is inside a catalog task- don't care which variable inside the MRVS changes, just that the MRVS is sending any update to the multi-row question answer table.  Also- we're on SanDiego now if that matters....  Thanks!

I think I am going to stick to the onLoad on this one.



Thanks for your input however.