Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

On Change Client script is causing a field to become Nan

bammar
Kilo Sage

I have an onchange Script that does a percent complet calculation based on when a particular field is update and then it updates the clients Percent complete.

A strange issue occurs, when the form loads, without any manipulation of any field let alone the field that has the OnChange script -   the percent complete changes to NaN after a fraction of a second.     My script will work to change the NaN to the right numbr but I am not sure why the Onchange script is causing my percentage complete to change on the client On Load . Has anyone seen this before?   when i comment out g_form.setValue ('percent_complete',strt ); its fine but of course the script wont work.   I am taking 2 duration parsing them into strings, getting out substrings to get the hours and minutes, doing   percent calculations- My Goal is using remaining time and actual time to run a calculation on the fly that shows percent complete on the client. Any words of help and advise would be appreciated,

1 ACCEPTED SOLUTION

Here you go. This solution is very similar to your other requirement. You need two onChange client scripts on the duration fields and a script include. You can reuse the same script include I gave you for your previous thread, just add one more function to the script include. Here u_dur_1,u_dur_2, u_percent are my field names



onChange client script on u_dur_1:


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


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


  return;


  }


  if(g_form.getValue('u_dur_2')!=''){


  var ga = new GlideAjax('CalculateDuration');


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


  ga.addParam('sysparm_dur1',newValue);


  ga.addParam('sysparm_dur2',g_form.getValue('u_dur_2'));


  ga.getXML(CallBack);


  }


  else{


  g_form.setValue('u_percent',100);


  }



  function CallBack(response) {


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


  g_form.setValue('u_percent',parseFloat(answer)*100);


  }


}



onChange client script on u_dur_2:


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


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


  return;


  }


  if(g_form.getValue('u_dur_1')!=''){


  var ga = new GlideAjax('CalculateDuration');


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


  ga.addParam('sysparm_dur2',newValue);


  ga.addParam('sysparm_dur1',g_form.getValue('u_dur_1'));


  ga.getXML(CallBack);


  }


  else{


  g_form.setValue('u_percent',0);


  }



  function CallBack(response) {


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


  g_form.setValue('u_percent',parseFloat(answer)*100);


  }



}



Script include:


var CalculateDuration = Class.create();


CalculateDuration.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  durationCalulator: function(){


  var duration = new GlideDuration(this.getParameter('sysparm_dur1') );


  var duration2 = new GlideDuration(this.getParameter('sysparm_dur2') );


  var answer = duration.subtract(duration2);


  return answer.getDurationValue();



  },


  percentCalculation: function(){


  var duration = new GlideDuration(this.getParameter('sysparm_dur1') );


  var duration2 = new GlideDuration(this.getParameter('sysparm_dur2') );


  var total=gs.dateDiff('1970-01-01 00:00:00',duration.getValue(),true);


  var answer = gs.dateDiff(duration2.getValue(),duration.getValue(),true);


  return (answer/total).toFixed(2);



  },


  type: 'CalculateDuration'


});


View solution in original post

11 REPLIES 11

fkhan
Kilo Guru

could you please post your script here.....


Sorry for the crazy formatting - By the way, I can see the correct number for percentage for a fraction of second- I can even do an alert and it will show my the right time, but the on change script changes the percentage complete field on load with no affect, if anything I want to change it to what it really is instead of blank. I am not sure why the system is changing that field when the field that is in the onchange hasnt been changed yet ....



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


  var strt = g_form.getValue('effort').toString();



  var hour1 = strt.substring(2,4);


  var min1 =   strt.substring(5,7);


  var sec1 =   strt.substring(8,10);



  var hour1num = parseInt(hour1, 10);


  var min1num = parseInt(min1,10);






  var end = g_form.getValue('work_effort').toString();



  var hour2 = end.substring(2,4);


  var min2=   end.substring(5,7);


  var sec2 =   end.substring(8,10);



  var hour2num = parseInt(hour2,10);


  var min2num = parseInt(min2,10);



  var reshour = parseInt(hour1num) - parseInt(hour2num);


  var resmin = parseInt(min1num) - parseInt(min2num);






  //percent


  var percenthour1 = parseInt(hour2num) * 60;


  var totalper1 = parseInt(percenthour1) + parseInt(min2num);




  var percentrem1 =   parseInt(reshour)* 60;


  var totalrem1 = parseInt(percentrem1) + parseInt(resmin);



  var percentgood =( parseInt(totalper1) /   (   parseInt(totalper1) +   parseInt(totalrem1)) * 100);



// var percentgood1 = parseInt(percentgood),10;



// if (isNAN ('percent_complete')){



  g_form.setValue('percent_complete',   percentgood);



}


Hi Ammar,


change these two lines in that code and try, why means you are convert them into string format.


var strt = g_form.getValue('effort').toString(); to var strt = g_form.getValue('effort')


var end = g_form.getValue('work_effort').toString(); to var end = g_form.getValue('work_effort')


BALAJI40
Mega Sage

HI Ammar,



NAN means not a number.


Simple on change client script, will have a block of


if (isLoading ) {


      g_form.setValue ('percent_complete',''); // Intially if you want to set empty value means try like that


          return;


    }


    //Type appropriate comment here, and begin script below


 


}



if still anything not working, post your code?