web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Sum multiple form fields and compare the total with another field

(0) ShareShare
ReportReport
Posted on by 27

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:

2772.example.png

 

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,

  • Suggested answer
    Tamilarasu Arunachalam Profile Picture
    593 on at

    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");
    }
     

    pastedimage1678879950622v1.png

    If you found this answer useful, please like and verify my answer

  • Suggested answer
    Ami K Profile Picture
    27 on at

    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");
              
            }
          
        }
      
    }
    

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
ParthPatel249 Profile Picture

ParthPatel249 64

#2
11manish Profile Picture

11manish 56 Super User 2026 Season 2

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 44 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans