Hi everyone,
I am trying to set up a field specific notification using java script based on a condition which should trigger on an On Change event on a form. The condition is to check if the sum of four number fields equal another field.
Unfortunately, I cannot use a Business Rule because the math operator is limited to two fields. My current workaround is to leverage a Power FX calculated column in the business rule. But this is not ideal, because this requires the user to save the form prior to the error message appearing. Below is an example of this in action currently:

I have gone via the java script route but unfortunately my js knowledge is limited. Here is what I have so far which is returning an error. Could you anyone suggest a correct js pattern?
function CheckCosts(executionContext) {
var formContext = executionContext.getFormContext();
var yr1 = Xrm.Page.getAttribute("cr37a_year1cost").getValue();
var yr2 = Xrm.Page.getAttribute("cr37a_year2cost").getValue();
var yr3 = Xrm.Page.getAttribute("cr37a_year3cost").getValue();
var yr4 = Xrm.Page.getAttribute("cr37a_remainingcostsoutsidecsrperiodyear4_base").getValue();
if (yr1 == null) return;
if (yr2 == null) return;
if (yr3 == null) return;
if (yr4 == null) return;
var sumYearlyCosts = yr1 yr2 yr3 yr4;
var CostsCovered = Xrm.Page.getAttribute("cr37a_costcovered").getValue();
var ShowErrMsg = sumYearlyCosts !== CostsCovered && sumYearlyCosts == 0;
if (formContext !== null && formContext != 'undefined')
{
var CostsCoveredControl = formContext.getControl("cr37a_costcovered");
if (ShowErrMsg) {
CostsCoveredControl.setNotification("The value entered does not equal the sum of the yearly breakdown.", "costcoveredcontrolnotification");
} else
{
CostsCoveredControl.clearNotification("costcoveredcontrolnotification");
}
}
}
JS code reference: https://www.c-sharpcorner.com/article/show-notifications-beside-crm-field-with-web-resource-in-dynamics-crm/
Best regards,