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.



tanguy
December 2, 2011
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.
greengrass
September 12, 2012
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.
Hosk
September 12, 2012
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
greengrass
September 12, 2012
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
greengrass
October 9, 2012
I got it to work now with passing execution context as first parameter in event handler properties. Thanks anyway.
Mouetton
November 6, 2012
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
Emil
February 22, 2013
It works ,, thank you