The CreatorCon Call for Content is officially open! Get started here.

Client script to check if more than one fields change on a form

antonymirza
Tera Expert

We can use onChange client script to check if any one of available fields on the form changes. I need to write an client script for two form fields, I mean script should run if any one of them changes.How can we achieve it?

1 ACCEPTED SOLUTION

As abhishekte suggests, you will need two client scripts on kb_knowledge, but you can use a third to put your key code in one place:



Name: FieldA Change


Active: Checked


Global: Checked


Type: onChange


Table: Knowledge [kb_knowledge]


Field name: FieldA


Script:


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


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


          return;


  }


  kbMyFunction();


}



Name: FieldB Change


Active: Checked


Global: Checked


Type: onChange


Table: Knowledge [kb_knowledge]


Field name: FieldB


Script:


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


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


          return;


  }


  kbMyFunction();


}



Then you need to define the shared function that both scripts will call, so create a third new Client Script:




Name: kbMyFunction


Active: Checked


Global: Checked


Type: onLoad


Table: Knowledge [kb_knowledge]


Script:


function onLoad() {


      // do nothing


}


function kbMyFunction() {


   


      var fielda = g_form.getvalue('u_fielda');


      var fieldb = g_form.getvalue('u_fieldb');


   


      if (fielda != '' && fieldb != '') {


              alert('Sending GlideAjax');


              var ga = new GlideAjax('HelloWorld');


              ga.addParam('sysparm_name','helloWorld');


              ga.addParam('sysparm_u_fielda',fielda);


              ga.addParam('sysparm_u_fieldb',fieldb);


              ga.getXML(HelloWorldParse);


      }


}


function HelloWorldParse(response) {


      var answer = response.responseXML.documentElement.getAttribute("answer");


      alert(answer);


}



See GlideAjax - ServiceNow Wiki for how to use GlideAjax.


View solution in original post

7 REPLIES 7

As abhishekte suggests, you will need two client scripts on kb_knowledge, but you can use a third to put your key code in one place:



Name: FieldA Change


Active: Checked


Global: Checked


Type: onChange


Table: Knowledge [kb_knowledge]


Field name: FieldA


Script:


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


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


          return;


  }


  kbMyFunction();


}



Name: FieldB Change


Active: Checked


Global: Checked


Type: onChange


Table: Knowledge [kb_knowledge]


Field name: FieldB


Script:


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


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


          return;


  }


  kbMyFunction();


}



Then you need to define the shared function that both scripts will call, so create a third new Client Script:




Name: kbMyFunction


Active: Checked


Global: Checked


Type: onLoad


Table: Knowledge [kb_knowledge]


Script:


function onLoad() {


      // do nothing


}


function kbMyFunction() {


   


      var fielda = g_form.getvalue('u_fielda');


      var fieldb = g_form.getvalue('u_fieldb');


   


      if (fielda != '' && fieldb != '') {


              alert('Sending GlideAjax');


              var ga = new GlideAjax('HelloWorld');


              ga.addParam('sysparm_name','helloWorld');


              ga.addParam('sysparm_u_fielda',fielda);


              ga.addParam('sysparm_u_fieldb',fieldb);


              ga.getXML(HelloWorldParse);


      }


}


function HelloWorldParse(response) {


      var answer = response.responseXML.documentElement.getAttribute("answer");


      alert(answer);


}



See GlideAjax - ServiceNow Wiki for how to use GlideAjax.


Dear Andrew & Abhishek,



Thank you for sharing your input and expertise. I am done with my requirement and it works



Brgds, AM


andrew.venables very very helpful, been looking for this for a few days. Thanks a ton !!!



Regards,


LG