CRM 2011 – Quick tip – Javascript to stop a form saving

This is probably something you will need to do at some point.  You want to validate a page and if it doesn’t fit certain criteria then the form should not be saved until the user has met your criteria.

To stop the save event in Javascript all you need is this

event.returnValue = false;

Tanguy the CRM tool guy (and CRM MVP) put something very interesting in the comments but I thought I would promote it to this very small blog post

The new method in CRM 2011 is the following one:

Xrm.Page.context.getEventArgs().preventDefault();

preventDefault : Cancels the save operation, but all remaining handlers for the event will still be executed.

13 thoughts on “CRM 2011 – Quick tip – Javascript to stop a form saving

  1. tanguy December 2, 2011 / 10:30 am

    The new method in CRM 2011 is the following one:

    Xrm.Page.context.getEventArgs().preventDefault();

    preventDefault : Cancels the save operation, but all remaining handlers for the event will still be executed.

    Like

  2. greengrass September 12, 2012 / 7:16 am

    Why can’t I get this to work? Neither the old or the new one works. Save will be completed even with this line. I also get an error if I use it with setFocus.

    Like

    • Hosk September 12, 2012 / 8:39 am

      have you tried this

      Xrm.Page.context.getEventArgs().preventDefault();

      if this does not work then maybe there is a problem with the javascript and the syntax is not correct. I would step through it using the debugger to make sure the line is being run

      Like

      • greengrass September 12, 2012 / 10:38 am

        This is peace of my code, that I have tested with on-premise with UR10

        //now with testing we only come here from case onsave
        //If subject IS parent, we need to notify user
        if (isParent) {

        alert(isParent); //true

        //After this alert, page saves normally
        alert(“Specify subject. Subject parent level is not allowed.”); //quoted text

        Xrm.Page.context.getEventArgs().preventDefault(); //error on page

        alert(“after preventing save.”);

        /*
        //This doesn’t work either
        if (event === “onsave”) {
        Xrm.Page.context.getEventArgs().preventDefault();
        }
        */
        //Xrm.Page.getAttribute(field).setValue(null);
        //alert(“After.”);
        //Xrm.Page.getControl(field).setFocus(true);
        }

        This is what happens on testing:
        I open case form (update form)
        I select parent level subject to subject field
        I click save
        I get alert: true
        I click ok
        I get alert: “Specify…”
        I click ok
        After that I get: error on page

        Like

  3. greengrass October 9, 2012 / 7:24 am

    I got it to work now with passing execution context as first parameter in event handler properties. Thanks anyway.

    Like

  4. Mouetton November 6, 2012 / 11:05 am

    Is it working for the entity Appointment ? Because my syntax working with the entity Account but the same JS doesn’t work for the appointment ? I don’t understand why :

    Here my syntax :

    function OnSave(ExecutionObj) {
    alert(‘error on save’);
    ExecutionObj.getEventArgs().preventDefault();
    }

    And I’ve already select : Pass execution context as first parameter

    Thanks

    Like

  5. Emil February 22, 2013 / 10:21 am

    It works ,, thank you

    Like

  6. Kalle August 31, 2013 / 6:43 pm

    Doesn’t seem to work (anymore?). I’m getting Object doesn’t support getEventArgs() -error.

    Like

  7. DeveloperM September 10, 2013 / 9:23 pm

    Seems to work inconsistently. About very other time I get a form error.

    Like

  8. Shaun Harrison June 5, 2014 / 3:12 pm

    Is there a way to execute more code if the user clicks cancel?

    Thanks

    Like

    • Hosk June 5, 2014 / 3:27 pm

      the code runs when the user presses the save button, there isn’t a cancel, unless you pop up an alert with yes/no on it.

      Like

      • S November 3, 2014 / 10:16 pm

        Xrm.Page.data.entity.addOnSave(function (execContext) {
        execContext.getEventArgs().preventDefault();//will prevent CRM form save
        });

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s