locked
Javascript: PreventDefault() doesnot prevent 'close' operation in CRM2016 RRS feed

  • Question

  • While working on an upgrade project from CRM2011 to CRM2016 on premise, we are facing below issue.

    On onsave event of an entity, if we prevent the save using 'executionObj.getEventArgs().preventDefault()' it only prevents the save operation and does not prevent close operation so when user click on  'Save and Close' button then it gives an alert message 'Your changes have not been saved. To stay on the page so that you can save your changes, Click Cancel' since preventDefault only prevent save operation and not the 'Close' operation. If anybody has any ideas/thoughts on this issue, please share.

    Thursday, April 21, 2016 10:47 AM

Answers

  • Hi,
    Using “executionObj.getEventArgs().preventDefault()”, we can only prevent CRM form from saving.
    However if you want to prevent form from closing then you can add an notification on any field on the same form using below code.

    function preventFormClose() {

    //setting notification on field to prevent close

    Xrm.Page.getControl(arg).setNotification(message,uniqueId)

    setTimeout(function(){

    //clearing notification from field so that user can make changes and save the changes again

    Xrm.Page.getControl(arg).clearNotification(uniqueId) } ,2000);

    }

    }

    The notification prevents the CRM form from saving and also prevent closing the form.




    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Rohit G Negi Monday, May 2, 2016 6:57 AM
    Friday, April 22, 2016 6:16 AM

All replies

  • Hi,
    Using “executionObj.getEventArgs().preventDefault()”, we can only prevent CRM form from saving.
    However if you want to prevent form from closing then you can add an notification on any field on the same form using below code.

    function preventFormClose() {

    //setting notification on field to prevent close

    Xrm.Page.getControl(arg).setNotification(message,uniqueId)

    setTimeout(function(){

    //clearing notification from field so that user can make changes and save the changes again

    Xrm.Page.getControl(arg).clearNotification(uniqueId) } ,2000);

    }

    }

    The notification prevents the CRM form from saving and also prevent closing the form.




    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Rohit G Negi Monday, May 2, 2016 6:57 AM
    Friday, April 22, 2016 6:16 AM
  • Thanks for your valuable response!! Solution provided above does prevent save and close but I observed few drawbacks with this workaround like.

    1) When we set notification to one of the field on form, then that notification on field is visible to end user. Even though, we are clearing the same after some time, it is still visible at lower bottom of form which can be annoying for end user. Any thoughts/ideas to avoid this.

    Our requirement is: We had few validations on onsave event and if those validation fails then we prevent the save. Now, while creating a new record, if user clicks 'Save and Close' then this gives unnecessary alert message.

    Looking for the solution for this as this was not the case in CRM2011.

    Monday, May 2, 2016 7:28 AM