locked
JavaScript OnChange Functions being called twice (2 times) in Microsoft Dynamics CRM Online 2015 Update 1 RRS feed

  • Question

  • I am experiencing JavaScript functions being called twice in Microsoft Dynamics CRM Online 2015 Update 1. Is anyone else having this issue? It's forcing me to put counters on all my functions to ensure they are only called once. Is there a fix?

    // Is this a bug?
    // OnChange function keeps getting called twice on my attribute
    if (oField.getValue() != null) {
        alert("Test " + oField.getValue());
        oField.setValue(null);
        oField.setValue("");
        alert("Test After Null Set " + oField.getValue());
        //Says the value is indeed null
    } else {
        alert(sErrorMsg); //never gets called
    }

    Bug in CRM Microsoft Dynamics CRM Online 2015 Update ?


    Jeremiah J. Isaacson

    Thursday, September 17, 2015 3:05 PM

All replies

  • Hello,

    The thing is you call setValue twice...

        oField.setValue(null);
        oField.setValue("");


    Dynamics CRM MVP
    My blog

    Thursday, September 17, 2015 3:12 PM
    Moderator
  • Hi Andrii,

    Thanks for the response. That doesn't resolve the issue. I've place that simply to show that I am resetting the value to empty/null. For whatever reason the function is still being called twice even after removing one or the other. Any other suggestions for this issue in in Microsoft Dynamics CRM Online 2015 Update 1? Thanks!


    Jeremiah J. Isaacson

    Thursday, September 17, 2015 3:39 PM
  • In this case could you please provide details how you register onchange event hander?

    Also could you please provide your full code?


    Dynamics CRM MVP
    My blog

    Thursday, September 17, 2015 3:42 PM
    Moderator
  • Below are images of the entire solution implemented. Screenshots are in order of occurrence:


    Jeremiah J. Isaacson

    Thursday, September 17, 2015 4:19 PM
  • (Additional images, max 9 per post in the forum...)


    Jeremiah J. Isaacson

    Thursday, September 17, 2015 4:20 PM
  • In the above example of this bug POC, my function was called 3 times.

    Jeremiah J. Isaacson

    Thursday, September 17, 2015 4:22 PM
  • Bug repo code (copy/paste):

    function OnChangeTest() {
        // Is this a bug in in Microsoft Dynamics CRM Online 2015 Update 1?
        // OnChange function keeps getting called twice on my attribute
        if (Xrm.Page.getAttribute("aaa_example").getValue() != null) {
            alert("Test " + Xrm.Page.getAttribute("aaa_example").getValue());
            Xrm.Page.getAttribute("aaa_example").setValue(null);
            alert("Test After Null Set " + Xrm.Page.getAttribute("aaa_example").getValue());
            //Says the value is indeed null
        } else {
            alert("Error"); //never gets called but should get called because I set the value to null.
        }
    }


    Jeremiah J. Isaacson



    • Edited by Jeremiah J. Isaacson Thursday, September 17, 2015 5:40 PM Wanted to note the CRM version in the comments.
    Thursday, September 17, 2015 4:28 PM
  • Do you resolver this bug?
    Saturday, July 8, 2017 11:11 PM
  • Do you resolver this bug?
    Saturday, July 8, 2017 11:12 PM
  • Hello Jeremiah!

    Do you solve this problem?

    I have similar issue in Microsoft Dynamics CRM 2016.

    Error message appears twice if I alert it on OnChange event and then set field value to null. It’s happen even I have first validated field to null.

    I notice that field clearing occurs a second time. So, my workaround is alert message only a second time. To resolve it I add error counter, that help to understand when really alert a message. I alert message only when counter is odd number.

    My solution:

    In MyEntity.FormEvents.js add counter on OnLoad event.

    function MyEntityOnLoad()
    {
        //Variable is needed to display Error Message only once.
        var ERROR_COUNTER = 0; 
    }
    

    Add function that call when need to show message and clean field.

    function _showError(errorMsg, fieldName)
        ///<summary>Display error message</summary>
        ///<param name="errorMsg" type="String">Error message</param>
        ///<param name="fieldName" type="String">Entity field name that need to clear</param>
    {
    
        //Display error message only if counter is odd number.
        if (ERROR_COUNTER != 0 && _isOdd(ERROR_COUNTER))
        {
            alert(errorMsg);
        }
        //Clear field value
        Xrm.Page.getAttribute(fieldName).setValue(null);
        ERROR_COUNTER++;
    }
    

    Add function that check is presented number odd or not.

    function _isOdd(num) 
    {
        ///<summary>Validate is passed odd number or not.</summary>
        ///<param name="num" type="number">Number</param>
        ///<return>true or false</return>
        return !!(num % 2);
    }
    

    It works for me.

    Friday, September 8, 2017 8:23 AM