Performing arithmetic functions on Currency fields within the browser (Adding fields and creating a total).

A customer had a requirement to sum the value of two currency fields and place the calculated total in a third field.

See the image below.

The three new fields in my example are

  • oppo_licensevalueestimate
  • oppo_servicevalueestimate
  • oppo_totalvalueestimate

Each have been defined as a Currency field and have been added to the screen OpportunityDetailBox.

The script that will enable the automatic update of the total field has been added to the Custom Content Box of the OpportunityDetailBox screen.

The code uses a mix of plain old JavaScript, the Client Side API and JQuery.

The follow assumptions have been made.

  • All calculations are to be performed in the screen as field values are changed.
  • The oppo_totalvalueestimate field is ReadOnly.
  • If a currency code is changed all currency codes will be set to the same value.

The main function will execute its code if the screen is generated by the system action 263 (Opportunuity Edit). It will set the total field (oppo_totalvalueestimate) to be readOnly then it will use JQuery to add onChange event functions to the currency value fields and the currency code fields.

The new onChange events on the currency value fields call a function (totalEstimates) that totals the fields and writes the value into the target field (oppo_totalvalueestimate).

The new onChange events on the currency code fields call a function (updateCurrency) that ensures all currencies used in the three fields are the same.

You can find the code below.

var updateCurrency = function (x) {
var currencyID = x.selectedIndex.valueOf();
document.getElementById("oppo_licensevalueestimate_CID").selectedIndex = currencyID;
document.getElementById("oppo_servicevalueestimate_CID").selectedIndex = currencyID;
document.getElementById("oppo_totalvalueestimate_CID").selectedIndex = currencyID;
}

crm.ready(function () {
var contextInfo = crm.getArgs();

//Set up Edit Oppo Screen
if (contextInfo.Act == "263") {
document.getElementById("oppo_totalvalueestimate").readOnly = true;

$("#oppo_licensevalueestimate").change(function () { totalEstimates(this) });
$("#oppo_servicevalueestimate").change(function () { totalEstimates(this) });

$("#oppo_licensevalueestimate_CID").change(function () { updateCurrency(this) });
$("#oppo_servicevalueestimate_CID").change(function () { updateCurrency(this) });
}
}
)

Anonymous