Calculation Jscript

One of the functions in CRM 2013 is to use Business Rules, but there is a limitation when it comes to Calculating multiple fields, I tried using them but ended up creating unnecessary fields and started over complicating things so guess what… I went back to old and faithful… JScript

So here was my scenario:

First I had to calculate 2 fields to a total field (new_TotalOne), then I had to calculate another two fields to another total field (new_TotalTwo), then I had to calculate all of the fields to get an overall total (new_OverallTotal)…

Doesn’t sound too complicated now does it? 🙂

Anyway this is what I did:

1. First I added the Web Resource to CRM, below the code:

function calculate() {
var val1 = Xrm.Page.getAttribute(‘new_field1’).getValue();
var val2 = Xrm.Page.getAttribute(‘new_field2’).getValue();
var val3 = Xrm.Page.getAttribute(‘new_field3’).getValue();
var val4 = Xrm.Page.getAttribute(‘new_field 4’).getValue();
var val5 = Xrm.Page.getAttribute(‘new_field 5’).getValue();
var val6 = Xrm.Page.getAttribute(‘new_field 6’).getValue();

{ var Total One = val1 + val2;
Xrm.Page.getAttribute(‘new_TotalOne’).setValue(totallicense);
Xrm.Page.getAttribute(‘new_TotalOne’).setSubmitMode(‘always’); — {The reason why I added this line was because if your field is read-only the field updates on the form but not in the database}
}

{ var totalenhancement = val3 + val4;
Xrm.Page.getAttribute(‘new_TotalTwo’).setValue(totalenhancement);
Xrm.Page.getAttribute(‘new_TotalTwo’).setSubmitMode(‘always’); — {The reason why I added this line was because if your field is read-only the field updates on the form but not in the database}
}

{ var result = val1 + val2 + val3 + val4 + val5 + val6;
Xrm.Page.getAttribute(‘newe_OverallTotal’).setValue(result);
Xrm.Page.getAttribute(‘new_OverallTotal’).setSubmitMode(‘always’); — {The reason why I added this line was because if your field is read-only the field updates on the form but not in the database}
}
}

2. The Second step was to add the Web Resource to the Form properties.

3. Then I added the onChange Event Handler “calculate” function to every value field so that my calculation happens whenever there is a change in one of the fields.

And there you go a not so complicated JScript to do some basic calculation for you, now the benefit of this is you can us any of the following – + / * to do your calculation.

Hope this will assist you…

You know you want to comment:

Website Powered by WordPress.com.

Up ↑