Question

I'd like to create a simple BMI calculator (MS Dynamics 2011)

I have 3 fields:

1: persons height (precision 2 - decimal formatted text box)
2. persons weight (precision 2 - decimal formatted text box)
3. BMI result - This field will display the BMI result. (also precision 2 - decimal)

I believe I need to check the oncreate event on both fields 1 & 2, to launch a javascript function to carry out the calculation. However, I am very new to both dynamics and java-script, and need some help.

I think something along these lines could be close. Can someone assist?

function BMICheck()
{
var Bmiresult = weight/(height/100*height/100);
Xrm.Page.getAttribute("new_bmiresulttextbox").setValue((Math.round(Bmiresult*100)/100).toString(    ));

}

I think I understand the logic OK, syntax is my main issue.

Thanks!

Updated answer with javascript:

function BMICheck()
{

var Bmival = Xrm.Page.getAttribute("crm_heightbox_decimal")/("crm_weightbox_decimal")/100*("crm_weightbox_decimal")/100);
 Xrm.Page.getAttribute("crm_bmiresultbox").setValue((Math.round(Bmival*100)/100));

 }
Was it helpful?

Solution

The javascript is ok, except for ToString as Daryl already pointed out. I would add a check for height and weight to prevent null values (the check if is a positive number can be avoided if the fields have a minimum value setting greater than 0.00)

function BMICheck()
{
    var weight = Xrm.Page.getAttribute("new_weight").getValue();
    var height = Xrm.Page.getAttribute("new_height").getValue();
    if (weight != null & height != null) {
        var bmi = weight/(height/100*height/100);
        Xrm.Page.getAttribute("new_bmi").setValue(Math.round(bmi*100)/100);
    }
    else {
        alert("Need to insert Weight and Height!");
    }
}

OTHER TIPS

Your javascript looks fine to me. The .toString() is unnecessary.

A helpful note about javascript and CRM, is to use F12 to debug your javascript in IE. You can set breakpoints and see what exactly is happening.

If it is generating an error, include it in your question.

  1. Learn how solutions work and work through a solution: http://www.dynamicscrmtrickbag.com/2011/05/28/dynamics-crm-2011-solutions-part-1/
  2. Add a JavaScript web resource to your new solution
    • Open your solution, or the default solution (through Customize the System)
    • Click Web Resources
    • Click New
    • Choose Script(JScript)
    • Click Text Editor and past your function in
  3. Add the web resource to the form
    • Open to form
    • Click Form Properties
    • Add the web resource as a library
  4. Add BMICheck to the onChange event on height and weight
    • Open the form
    • Click the height field
    • Click Change Properties
    • Click Events
    • Add your function
    • Repeat for the weight field
    • NOTE: make use of Guido's null check to avoid errors
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top