The CreatorCon Call for Content is officially open! Get started here.

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

Abhinay Erra
Giga Sage

Before proceeding further, I would like to ask what are the field types involved here?


The onload return did help to keep the original percent value when the form loads, but the NAN does appear now when doing the calculation




Actual effort — duration



Remaining effort - duration



Percent complete = percent



Bessam Ammar


Platform Analyst - IT


Florida Virtual School


2145 MetroCenter Blvd, Suite 200


Orlando, FL 32825


Office: (407) 513-3666


Email: bammar@flvs.net<mailto:bammar@flvs.net>


Web: www.flvs.net



Please Consider the environment before printing this email.


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'


});


Hello,



Thank you so much again… you know the more I though about it essentially what needs to be done is duration division…. I will try this and update you sir.



Bessam Ammar


Platform Analyst - IT


Florida Virtual School


2145 MetroCenter Blvd, Suite 200


Orlando, FL 32825


Office: (407) 513-3666


Email: bammar@flvs.net<mailto:bammar@flvs.net>


Web: www.flvs.net



Please Consider the environment before printing this email.


This is so close but its not doing the percent calculation




I need ( actual effort / ( actual +remaining) ) as the calculation being done in scrip include




So if I have actual work (parm1) as 1 hour and remaining (parm2) as 4 then (1 / (1+4) = 1/5 = 2 percent time worked. The hard part for me is the syntax for all the math in the script include.



Bessam Ammar


Platform Analyst - IT


Florida Virtual School


2145 MetroCenter Blvd, Suite 200


Orlando, FL 32825


Office: (407) 513-3666


Email: bammar@flvs.net<mailto:bammar@flvs.net>


Web: www.flvs.net



Please Consider the environment before printing this email.