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.
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.
LikeLike
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.
LikeLike
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
LikeLike
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
LikeLike
I got it to work now with passing execution context as first parameter in event handler properties. Thanks anyway.
LikeLike
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
LikeLike
It works ,, thank you
LikeLike
Doesn’t seem to work (anymore?). I’m getting Object doesn’t support getEventArgs() -error.
LikeLike
Seems to work inconsistently. About very other time I get a form error.
LikeLike
Is there a way to execute more code if the user clicks cancel?
Thanks
LikeLike
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.
LikeLike
Xrm.Page.data.entity.addOnSave(function (execContext) {
execContext.getEventArgs().preventDefault();//will prevent CRM form save
});
LikeLike