I use the following condition to check a,b values before save.And I call this function form's onsave Event.
if the field values a == b means i need to save the record.Else I need to show the alert.
if a!= b condition the infinite loop will occur which means the alert message shows continuously .My problem is i need to prevent save record if(a!=b) . I used eventArgs.preventDefault(); but its not working.
Am working in CRM 2016.
function onsavecall()
{
if(a != b)
{
alert("alert a is not equal to b .so please change value B");
}
else
{
Xrm.Page.data.save.then(function(){
Xrm.Page.data.refresh();
});
}
}
Thanks in Advance
*This post is locked for comments
I've found the easiest way to restrict saving is by setting notifications on form control, as a bonus you can display to the user way they can't save:
if(foo) {
formContext.getControl("rsf_type").setNotification('Cannot enter a PTO. No remaining balance.');
} else {
formContext.getControl("rsf_type").clearNotification();
}
setNotification will block saving.
preventDefault is not necessary, nor is entity.save.
All you need is setNotification if data is bad, and clearNotification if it's good.
Hello everyone!
I got the functionality to work and successfully prevent a save. (In this case, I'm trying to make sure a date is in the future.)
Here's my code:
function checkEstCloseDate(context) { var saveEvent = context.getEventArgs(); var today = new Date(); if (Xrm.Page.getAttribute("estimatedclosedate").getValue() < today) { Xrm.Page.getControl("estimatedclosedate").setNotification("Est Close Date is in the past. Please change it to a future date.", "1"); saveEvent.preventDefault(); } else { Xrm.Page.ui.clearFormNotification("1"); Xrm.Page.data.entity.save(); } }
The above code successfully checks the date and applies the error message.
The problem arises when I try to change the date in the field. My code does not recognize a change has been made, and thus does not turn off the error. How do I fix this? I want to be able to change the value of the field and have the error message go away so I can save the form.
function onsave(context) {
// Prevent the event execution
context.getEventArgs().preventDefault();
}
Hi Please try this code
//Save Form changes Using JS
Xrm.Page.data.entity.save();
Hey Vijay,
Any updates on the answers that are specified below. If any given solution has worked for you please mark that post as answer. Or else let us know what is bugging still in the code?
We will help you out.
-Prashant
I have described the approach in this blog post.
neilparkhurst.com/.../javascript-prevent-save
First you need to select the option which is "Pass execution contact as first parameter".
Then you will need code something like .....
function onSave(context) {
var saveEvent = context.getEventArgs();
if (<<a>>!=<<b>>) {
alert("Put in a phone number!");
saveEvent.preventDefault();
}
}
Hi Vijay,
Check the below code :
function onSaveCall(context)
{
var a= Xrm.Page.getAttribute("fieldname").getValue();
var b= Xrm.Page.getAttribute("fieldname").getValue();
if(a !=b)
alert( "Unable to save as values are not equal..." );
context.getEventArgs().preventDefault();
}
Then onSave event call this function & check the checkbox"Pass execution context as first parameter".
You can check this link :www.infinite-x.net/.../crm-2011-canceling-the-save-operation
Thanks
Hi Vijay,
Use this code, Trigger a method on save and pass the context.
function onSave(context) {
if(a != b)) {
context.getEventArgs().preventDefault();
}
}
Hope this helps you !
Thanks
Hemant
Hi Vijay12872
It looks like eventArgs.preventDefault() is not working.
Make sure you check the "Pass execution context as first parameter" while adding your function to onSave event handlers.
Thanks
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156