CRM 2011 How do I work out pecentages with jscript

Hi I have a few custom currency fields on my quotes form, and preform some simple calculations on them, providing there is a value greater than £0.00 in both my fields all works ok, if they are both set to £0.00 I get an error when saving as it looks like my pecentage field is populated with a 0 but it is actually blank.

function quotecalc ()


var topersonnel = Xrm.Page.getAttribute("new_turnovervaluepersonnel").getValue();
var toequipmenthire = Xrm.Page.getAttribute("new_turnovervalueequipmenthire").getValue();
var tofabriction = Xrm.Page.getAttribute("new_turnovervaluefabrication").getValue();
var torechargeables = Xrm.Page.getAttribute("new_turnovervaluerechargeables").getValue();
var toengineering = Xrm.Page.getAttribute("new_turnovervalueengineering").getValue();
var tosobi = Xrm.Page.getAttribute("new_turnovervaluesobi").getValue();

var gppersonnel = Xrm.Page.getAttribute("new_gpvaluepersonnel").getValue();
var gpequipmenthire = Xrm.Page.getAttribute("new_gpvalueequipmenthire").getValue();
var gpfabriction = Xrm.Page.getAttribute("new_gpvaluefabrication").getValue();
var gprechargeables = Xrm.Page.getAttribute("new_gpvaluerechargeables").getValue();
var gpengineering = Xrm.Page.getAttribute("new_gpvalueengineering").getValue();
var gpsobi = Xrm.Page.getAttribute("new_gpvaluesobi").getValue();

var pcpersonnel = (gppersonnel / topersonnel) * (100);

var pcequipmenthire = (gpequipmenthire / toequipmenthire) * (100);

var pcfabrication = (gpfabriction / tofabriction) * (100);

var pcrechargeables = (gprechargeables / torechargeables) * (100);

var pcengineering = (gpengineering / toengineering) * (100);

var pcsobi = (gpsobi / tosobi) * (100);


Open in new window

Who is Participating?
Michel PlungjanConnect With a Mentor IT ExpertCommented:
There is no point in using parseFloat on a calculation.

I suggest something like this
if (isNaN(gppersonel)  || gppersonel  == "") gppersonel =0;
if (isNaN(topersonnel) || topersonnel == "") topersonnel=0;

var pcpersonnel = gppersonel!=0 && topersonnel!=0?((gppersonnel / topersonnel) * 100):0;

Open in new window

Feridun KadirConnect With a Mentor Principal ConsultantCommented:
Isn't this a case of not being allowed to divide by zero?  I think you should tests for the value of each turnover field and if it is zero then set the percentage to zero, otherwise perform the calculation.

That sounds like a logical approach.  Most applications will crash on a zero divide, but a brwser will tolerate almost any error and do the best it can to keep going, blanking the fieds on error makes sense.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Mark BradyPrincipal Data EngineerCommented:
I wouldn't blank them I would set them to 0% so it looks right to the end user.
rhill52Author Commented:
Thanks for your help in the end I used below to correct the divide by 0 issue.

var pcpersonnel = (gppersonnel / topersonnel) * (100);
pcpersonnel = pcpersonnel || 0;
Michel PlungjanIT ExpertCommented:
Great you fixed it.

Still no need to use parseFloat. If you want decimals, use toFixed(numberOfDecimals)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.