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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Feridun KadirPrincipal 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.
Mark BradyPrincipal Data EngineerCommented:
I wouldn't blank them I would set them to 0% so it looks right to the end user.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Michel PlungjanIT 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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Dynamics

From novice to tech pro — start learning today.