Plugin throw customized error, unsaved changes are on form.

I have got one issue when I change some hidden fields on the CRM form and then I was executing the save code for CRM, on save there was one plugin running that throws some handled error message. Now I only get the “OK” and “Download Log” button.

Once i click OK button i still see some unsaved changes

For that if we don’t want to show the unsaved changes on the form below things we can do.

  1. Either find what are all the fields we have changes and set their initial values once you click the okay button (Note: we don’t have any click event given for this ‘OK’) button.
  2. Or some how set the form dirty = false on close of that pop up. (That is also not possible to achieve as we don’t have click/change event)

Solution for this situation would be as below :

  1. we can use save.then() function like below:

Xrm.Page.data.save().then(function (success) {
//This is success call back , do whatever you want to do. after save is done.
}, function (error) {
Xrm.Page.getAttribute(“FieldUniqueName”).setValue(Xrm.Page.getAttribute(“FieldUniqueName”).getInitialValue());

////Please note this getInitialValue only work for optionset and bool fields.
Xrm.Page.data.setFormDirty(false);
if (error && error.message) {
//This is the section where we get the error.message in the string format, we can show this in the formnotification.
} else {
// show default message if the error.message is empty or undefined.
}
});

Advertisements

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