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

If you want to calculate the percent completed. It should be (Actual-Remaining)/Actual. And I have tested this, it should work


Abhinay Erra
Giga Sage

Can you mark my response with the script as correct so that others who have same question can find the correct answer easily.