Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Sum multiple form fields and compare the total with another field

Posted on by 100

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
    Ami K Profile Picture
    Ami K 100 on at
    RE: Display field specific notification using java script based on condition

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

  • Suggested answer
    RE: Display field specific notification using java script based on condition

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans