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

antonymirza
Tera Expert

Experts please share your input...


Hi,



Unfortunately,you have to write two client each on every field to run the same.There is no such way, as per my thinking.


andrew_venables
ServiceNow Employee
ServiceNow Employee

g_form has a couple of undocumented things that should allow you to do this:



  1. g_form.modified : Boolean True/False
  2. g_form.modifiedFields : Object

  3. g_form.modifiedFields could contain something like:
  4.     incident.caller_id: true

  5. You can access g_form from anywhere on the Client-side.

  6. Bear in mind this undocumented functionality so it might change in a future release

Dear Andrew,



Thank you for your input but I am not getting how and where to start my script. I have to write a script on KB form (kb_knowledge) and should execute when either field A or field B on the form changes.



If I go with onChange client script then I am not able to satisfy above said condition of either field A or field B on the form changes. Since, both of these fields will be used in my GlideAjax call therefore, I do not opt writing two onChange client scripts.



Can you please suggest?



Brgds, AM