reload form on client script

sangeethasingh1
ServiceNow Employee
ServiceNow Employee

Hi,

I wrote onchange client script which checks if the value in a filed   is valid or not. If it is invalid i need to reload the form so that it get the previous value on that field.

How should i do that in client script. Please let me know.

Thanks

9 REPLIES 9

Hi Chuck,



function isOrderSubmitted () {


  if(g_form.getValue('status') == 'order_submitted' || g_form.getValue('status') == 'order_approved' || g_form.getValue('status') == 'accepted' || g_form.getValue('status') == 'rejected' || g_form.getValue('status') == 'cancelled')


                {


            g_form.addErrorMessage('Sales Type Value cannot be altered once Order is submitted. ');


                      g_form.setValue('partnership_type',oldValue);


                          return true;


                }



}



When i set the value here it is triggering into infinite loop. Just tested it.



Thanks


Don't return true, just return, or return false.


FYI - returning true is typical programmer speak for "everything worked, I'm good - go on and do what you need to do." Returning false, on the other hand, is a convention for saying "something went wrong."


I have to admit, I didn't see the function "isOrderSubmitted()" and hope that this is enclosed within the standard onChange template function. If not, your function should look like this to provide you with the newValue and oldValue stuff...



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


  if (isLoading || newValue === '') {


            return;


  }



  if(g_form.getValue('status') == 'order_submitted' || g_form.getValue('status') == 'order_approved' || g_form.getValue('status') == 'accepted' || g_form.getValue('status') == 'rejected' || g_form.getValue('status') == 'cancelled')


  {


            g_form.addErrorMessage('Sales Type Value cannot be altered once Order is submitted. ');


            g_form.setValue('partnership_type',oldValue);


            return;


  }



}


venkatiyer1
Giga Guru

How are you setting the old value, if you are using g_form.setValue it would trigger the loop. Try either control.value = oldValue or alternatively just return



  1. if (isLoading || newValue === '' || newValue is Invalid) {  
  2.           return;  
  3.     }