locked
getEventArgs().preventDefault not working RRS feed

  • Question

  • Hi,

    I replaced event.returnValue with getEventArgs().preventDefault() but it is not working.  Please find below the scripts and screen-shots for you reference.  It give error and save record.  Please help to find solution.

    function Form_onsave(executionOb)

    {

    if((Xrm.Page.getAttribute("new_accountid").getValue() != null ) &&

    (Xrm.Page.getAttribute("new_name").getValue() == null ))

    {

     var results = GetRelatedcontacts();

     if (results != 0)

       {

       alert("Technology details of this account is already in CRM. Please close this window and select existing record from view");

               Xrm.Page.getAttribute("new_name").setValue() =null;

               Xrm.Page.getControl("new_accountid").setFocus(true);

               executionObj.getEventArgs().preventDefault();

             }

       }

    }

    It display the alert message and give below error but save record.


    pnoushu@hotmail.com

    Wednesday, February 8, 2017 3:21 PM

All replies

  • You need to put alert on every line of code so that you can be sure about from where the error is coming.

    Use any of following command:-

    Xrm.Page.ui.tabs.get("new_accountid").setFocus();
    Xrm.Page.getControl("new_accountid").setFocus();


    Regards Faisal

    Wednesday, February 8, 2017 3:45 PM
  • You are passing parameter as 'executionOb' where as you are using it as 'executionObj', make sure that you are using same parameter name and it should work

    MaKeer

    Wednesday, February 8, 2017 7:05 PM
  • Hi,

    I have corrected parameter name.  but I am getting the below error please check and save the record.  Do I have declare the variable in script.

    function Form_onsave(executionObj)
    {

    if((Xrm.Page.getAttribute("new_accountid").getValue() != null ) &&
    (Xrm.Page.getAttribute("new_name").getValue() == null ))
    alert("1");
    {

    var results = GetRelatedcontacts();

     if (results != 0)
       {
         alert("2");
      alert("Technology details of this account is already in CRM. Please close this window and select existing record from view");
               Xrm.Page.getAttribute("new_name").setValue(null);
               Xrm.Page.getControl("new_accountid").setFocus(true);
               executionObj.getEventArgs().preventDefault();

          } 
      }

    }


    pnoushu@hotmail.com

    Thursday, February 9, 2017 8:46 AM
  • I tried with following simple JS code and it worked for me. Probably error is coming some where else. 

    Try Publishing All Customization and then refresh the CRM form multiple time just to make sure that browser is loading latest JS file.

    If nothing works try Client debugging to see whats going on on line by line basis

    function Form_onsave(executionObj)
    {
     alert("Form_onsave");
     executionObj.getEventArgs().preventDefault();
    }



    MaKeer

    Thursday, February 9, 2017 8:02 PM
  • Hi,

    Please check auto save option is enable or disable.

    Regards,

    Noushad


    pnoushu@hotmail.com

    Sunday, February 12, 2017 1:13 PM
  • OnSave should work even with Auto Save, as per SDK documentation

    The OnSave event occurs when:

    • With auto-save is enabled, 30 seconds after data has changed and there is unsaved data in the form.

    Do you have Auto Save enabled?


    MaKeer

    Sunday, February 12, 2017 5:47 PM
  • Hi,

    Thanks for reply.  I have event.returnValue commands in field change events also.  Can I replace  event.returnValue  with getEventArgs().preventDefault();

    function new_entreminder_onchange()
    {

    if (Xrm.Page.getAttribute("new_entcreatedon").getValue() == null)
    {
    alert("Save Account technology details then give reminder")
    Xrm.Page.getAttribute("new_entreminder").setValue() = null;
    event.returnValue=false;
    }

    }

    As mentioned below

    function new_entreminder_onchange(executionObj)
    {

    if (Xrm.Page.getAttribute("new_entcreatedon").getValue() == null)
    {
    alert("Save Account technology details then give reminder")
    Xrm.Page.getAttribute("new_entreminder").setValue() = null;

    executionObj.getEventArgs().preventDefault();

    }

    }


    pnoushu@hotmail.com

    Monday, February 13, 2017 5:51 AM
  • Hai Noushad,

    Close all the browsers...then clear your browser history...clear the temp data. Now again login and check the code...it should work.

    Monday, February 13, 2017 10:39 AM