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,
Thanks Tamilarasu Arunachalam but the question was less to do with how to display a notification but rather how to sum multiple fields into a total (I have updated the title of this post).
Fortunately I have figured out the problem with my code and this is now working.
function CheckCosts(executionContext) { var formContext = executionContext.getFormContext(); if (formContext !== null && formContext != 'undefined') { 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').getValue(); var sumYearlyCosts = yr1 yr2 yr3 yr4; var costcovered = formContext.getAttribute("cr37a_costcovered").getValue(); var costcoveredcontrol = formContext.getControl("cr37a_costcovered"); var costvalue = Xrm.Page.getAttribute("cr37a_costcovered").getValue(); if ( (sumYearlyCosts !== costvalue )) { formContext.ui.setFormNotification("The value entered does not equal the sum of the yearly breakdown.", "WARNING", "CostMandateNotification"); costcoveredcontrol.setNotification("The value entered does not equal the sum of the yearly breakdown.", "costcontrolnotification"); } else { formContext.ui.clearFormNotification("CostMandateNotification"); costcoveredcontrol.clearNotification("costcontrolnotification"); } } }
Hi Imran,
you have to use the function in onChange event of the field to get Notification whenever that field value gets changed
Please refer the below code
function fieldNotification(executionContext) { var formContext = executionContext.getFormContext(); formContext.getControl("name").setNotification("Error", "123444"); }
If you found this answer useful, please like and verify my answer
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156