Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Hi Daniel ,



I am assuming that this Client script is onChange of field "u_cost_object". If this is the case then:



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


      if (isLoading){


              return;


      }



// below line added to check onChange and newValue is not blank and null


if(!isLoading && newValue != '' || newValue != null){


  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



You can refer Client Scripts - ServiceNow Wiki for more clarity


Please mark answer correct if it was really helpful.


Thanks,


Darshan


dan_cooper
Kilo Contributor

Thanks Darshan,


Unfortunately that doesn't seem to resolve the issue - the checkSAPInfo() function is still being called twice.



Thanks for your help,


Dan


Hi Daniel,



Minor change in Darshan Rathod's code.



if(!isLoading && newValue != null && newValue != '' ) {


//rest of the code


}




Thanks


Mandar



EDIT : is u_cost_object a boolean field? In that case you can set it to "false" and make changes in above conditions accordingly.


dan_cooper
Kilo Contributor

Ah hah!   That seems to have done the trick.   Thanks so much Mandar!


-Dan