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);
alert(pcpersonnel);
Xrm.Page.getAttribute("new_gppecentagepersonnel").setValue(parseFloat(pcpersonnel));

var pcequipmenthire = (gpequipmenthire / toequipmenthire) * (100);
Xrm.Page.getAttribute("new_gppecentageequipmenthire").setValue(parseFloat(pcequipmenthire));

var pcfabrication = (gpfabriction / tofabriction) * (100);
Xrm.Page.getAttribute("new_gppecentagefabrication").setValue(parseFloat(pcfabrication));

var pcrechargeables = (gprechargeables / torechargeables) * (100);
Xrm.Page.getAttribute("new_gppecentagerechargeables").setValue(parseFloat(pcrechargeables));

var pcengineering = (gpengineering / toengineering) * (100);
Xrm.Page.getAttribute("new_gppecentageengineering").setValue(parseFloat(pcengineering));

var pcsobi = (gpsobi / tosobi) * (100);
Xrm.Page.getAttribute("new_gppecentagesobi").setValue(parseFloat(pcsobi));

}

Open in new window

image1.JPG
image2.JPG
rhill52Asked:
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;
Xrm.Page.getAttribute("new_gppecentagepersonnel").setValue(pcpersonnel.toFixed(2));

Open in new window

0
 
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.
0
 
COBOLdinosaurCommented:
@feridun,

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.
0
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.
0
 
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;
//alert(pcpersonnel);
Xrm.Page.getAttribute("new_gppecentagepersonnel").setValue(parseFloat(pcpersonnel));
Xrm.Page.getAttribute("new_gppecentagepersonnel").setSubmitMode("always");
0
 
Michel PlungjanIT ExpertCommented:
Great you fixed it.

Still no need to use parseFloat. If you want decimals, use toFixed(numberOfDecimals)
0
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.