Automatically clear values in several fields when one field is cleared

Debbi L
Tera Expert

I have a form that, in part, uses four related fields to identify where a piece of hardware is located, and what type it is. I've written a client script that will clear all four fields when one (business_unit) is cleared.

Script Type = onChange

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading) {

          return;

    }

    g_form.setValue('district', '')

    g_form.setValue('area', '')

    g_form.setValue('structure_type', '');

}

This works exactly as it's supposed to. Because you can only query one field in a client script, I've written additional scripts on district, area, and structure_type fields. They mirror the original script, only changing the setValue field names. As you can imagine, once I've done that, it's a race to see which script works (or doesn't).

I receive this error if I activate any of the addition scripts:

onChange script error: RangeError: Maximum call stack size exceeded function (){var o=i(m,arguments);return l.apply(n,o)}

Not much of a surprise, as I should be able to find some way to do this in a single script. Therein lies my challenge - I have no idea where to start. Please help?

Thanks,

Debbi

1 ACCEPTED SOLUTION

I think you might be hitting an infinite loop. Try adding (for each variable you're trying to update in each script):



if(g_form.getValue('business_unit')!=''){


g_form.setValue('business_unit','');


}


View solution in original post

5 REPLIES 5

kristenankeny
Tera Guru

It looks like your script is missing some semi-colons. Not sure if that's because it's missing in your actual script or just in what you put here.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading) {


          return;


  }


  g_form.setValue('district', '');


  g_form.setValue('area', '');


  g_form.setValue('structure_type', '');


}



I would also suggest adding else if(newValue == '')



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading) {


          return;


  }


else if(newValue == ''){


g_form.setValue('district', '');


  g_form.setValue('area', '');


  g_form.setValue('structure_type', '');


}


}


Thanks, Kristen. This script actually works. It's when I add the second script, which uses the same set of four fields, but queries a different one, that it fails.



Script #2, using the "District" field (I added your recommendations to both):



  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {  
  2.   if (isLoading) {  
  3.           return;  
  4.   }  
  5. else if(newValue == ''){  
  6. g_form.setValue('business_unit', '');  
  7.   g_form.setValue('area', '');  
  8.   g_form.setValue('structure_type', '');  
  9. }  
  10. }  

I think you might be hitting an infinite loop. Try adding (for each variable you're trying to update in each script):



if(g_form.getValue('business_unit')!=''){


g_form.setValue('business_unit','');


}


That did the trick, thank you!



Used the same script (with the different fields) for all three, no errors, and works exactly as it should. Thanks for your help!



Final script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {  


  if (isLoading) {  


          return;  


  }  


else if(newValue == ''){  


  if(g_form.getValue('district')!=''){


  g_form.setValue('district','');


}


  if(g_form.getValue('area')!=''){


  g_form.setValue('area','');


}


  if(g_form.getValue('structure_type')!=''){


  g_form.setValue('structure_type','');


}


}  


}