Answered by:
Stop saving a form using JavaScript, MS CMR 2011

Question
-
Hi,
eventObj.getEventArgs().preventDefault(); is the code snippet used to stop saving a form.
i wanted to use this line in callback function. any idea to achieve this ?
i have tried the below code which is not working.
even if retrieved.results.lenght > 0, user can save a record.
function GetBankAccountBalance(event) { var serverUrl = Xrm.Page.context.getServerUrl(); var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } var retrieveReq = xmlhttp; var Odata = serverUrl + "xrmservices/2011/OrganizationData.svc/accountSet?$select=name"; retrieveReq.open("GET", Odata, true); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this, event); }; retrieveReq.send(); retrieveReqCallBack(retrieveReq); } function retrieveReqCallBack(retrieveReq, event) { if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d; if(retrieved.results.length>0){ eventObj.getEventArgs().preventDefault(); } } } }
Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
- Edited by Ravitheja J Monday, July 8, 2013 10:40 AM
Monday, July 8, 2013 10:39 AM
Answers
-
Ravitheja, yes, I realize that you already changed the eventObj to event I was just noting it again for completeness. The main issue that is causing your form to still save is that you are executing the callback asynchronously. Please see my previous post for the change required to execute synchronously.
Nick
- Edited by Nick Doriot, Hitachi Solutions America, Ltd Monday, July 8, 2013 11:51 AM correction
- Marked as answer by Ravitheja J Monday, July 8, 2013 12:27 PM
Monday, July 8, 2013 11:51 AM
All replies
-
Debug your code also can try code:
window.preventSave = true; return
Hope this helps. ----------------------------------------------------------------------- Santosh Bhagat If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"
Monday, July 8, 2013 10:53 AM -
use
event.getEventArgs().preventDefault();
instead of
eventObj.getEventArgs().preventDefault();
I hope you get it.
- Proposed as answer by myCRMGuy Monday, July 8, 2013 10:57 AM
Monday, July 8, 2013 10:57 AM -
Hi Thanks for your reply,
I have tried
event.getEventArgs().preventDefault();
instead of
eventObj.getEventArgs().preventDefault();
no success still.!
Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
Monday, July 8, 2013 10:58 AM -
Did you try debugging and check Event object?Monday, July 8, 2013 11:03 AM
-
Hi
yeah,
i tried debugging my code.! its not giving me any kind of errors. execution is going on to next line and record is getting saved.
Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
Monday, July 8, 2013 11:15 AM -
This code is running inside the OnSave event?
You set the first parameter in this way?
My blog: www.crmanswers.net
Monday, July 8, 2013 11:33 AM -
You have a couple of issues here. First, your XMLHttpRequest is executing asynchronously. Since it is executing asynchronously, your GetBankAccountBalance function is finishing execution before the response is received and the form is then saved since the callback hasn't been executed and the event has not been cancelled. The easiest way to make it work would be to make the call execute synchronously by changing the last argument of your XMLHttpRequest open call to false, e.g. retrieveReq.open("GET", Odata, false).
The other issue is the your usage of eventObj instead of event. Your local copy of the CRM event object in your callback is event and the you should call event.getEventArgs().preventDefault().
Nick
- Proposed as answer by Nick Doriot, Hitachi Solutions America, Ltd Monday, July 8, 2013 11:35 AM
Monday, July 8, 2013 11:35 AM -
Hi Nick,
as i mentioned earlier, i have changed eventObj to event and checked. still record is getting saved.
Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
Monday, July 8, 2013 11:45 AM -
Hi Guido,
yeah. this code is running inside OnSave event and i have checked that option to pass execution context as first parameter.
Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.
Monday, July 8, 2013 11:47 AM -
Ravitheja, yes, I realize that you already changed the eventObj to event I was just noting it again for completeness. The main issue that is causing your form to still save is that you are executing the callback asynchronously. Please see my previous post for the change required to execute synchronously.
Nick
- Edited by Nick Doriot, Hitachi Solutions America, Ltd Monday, July 8, 2013 11:51 AM correction
- Marked as answer by Ravitheja J Monday, July 8, 2013 12:27 PM
Monday, July 8, 2013 11:51 AM -
Xrm.Page.getAttribute([fieldname]).setSubmitMode("never");
the setSubmitMode takes 3 different parameters
"never" -- not to save data
"always" -- always save data regardless the change
"dirty" -- save the data if it is dirtySkype:vin.k.s@live.in
Monday, July 8, 2013 12:48 PM