onChange client script triggered twice

dan_cooper
Kilo Contributor

My script resets the value of the field to null if the server returns a negative result, and it appears that that reset is causing the onChange function to be triggered again.   Anyone else have this problem?   Any suggestions to resolve?

8 REPLIES 8

ohhgr
Kilo Sage
Kilo Sage

Hi Daniel,



Check if there's any looping going on in this case.



Like onChange of a field you're setting some other field value. And on change of that other field, your field is getting changed.



Such things might cause the issue you're facing.



Thanks,
Mandar


dan_cooper
Kilo Contributor

I deactivated all other client scripts for this form, and this is the only field affected by this script.


darshanr
Kilo Guru

Hi Daniel,



Make use of parameters oldValue and newValue in onChange Client script:



For Eg:


if(oldValue != newValue && newValue != null)



If it stiil persists,for more clarity can you paste you Cleint script.



Thanks,


Darshan


dan_cooper
Kilo Contributor

Hi Darshan,


The client script is pasted below.  


Darshan and Mandar, thanks for your input!


-Dan





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


      if (isLoading){


              return;


      }


  var costInfo = checkSAPInfo();


  var costObject = g_form.getValue('u_cost_object');


  var deptID = g_form.getReference('u_requesting_department');


  var deptNumber = deptID.name;




  if (costObject!='' && deptNumber!=''){


  if (costInfo != "true"){


  alert("This is not a valid cost object.   Please contact your business officer to obtain a valid cost object.");


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


  }


  else{}


  }


}




function checkSAPInfo(){


  var costObject = g_form.getValue('u_cost_object');


  var deptID = g_form.getReference('u_requesting_department');


  var deptNumber = deptID.name;



      var ga = new GlideAjax('sapInfo'); // the Script Include object name


  ga.addParam('sysparm_name','getRecordByID'); // the function on the server to call


  ga.addParam('sysparm_svc', "SAPCostObject");


  ga.addParam('sysparm_method', "wsSoap12.ValidateDepartmentAndCostObject");


  ga.addParam('sysparm_node', "ValidateDepartmentAndCostObjectResponse");


  ga.addParam('sysparm_arg1Name', "departmentNumber");


  ga.addParam('sysparm_arg1Value', deptNumber);


  ga.addParam('sysparm_arg2Name', "costObjectCode");


  ga.addParam('sysparm_arg2Value', costObject);


   




              ga.getXMLWait();


  return(ga.getAnswer());


}