web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Maintaining disabled field state when saving and when save is cancelled by a plugin

(0) ShareShare
ReportReport
Posted on by

We have javascript on our form that runs in the onchange event of a field. It sets a second field's value based on the first field's value, then disables the 2nd field.

When the form is saved, we have found that the values of disabled fields are not committed regardless of their dirty state. To work around this issue in 2011, we enable the field during the form's Save event, then disable the field once again during the form's Load event.

In 2016, this no longer works because the form is not refreshed after the Save operation, and therefore the form Load event does not fire after Save.

One workaround is to use setSubmitMode() and always submit the field's value, but this can fire plugins and workflows unnecessarily when the field's value does not change.

Another workaround is to include the modified attribute on the form, set its visibility to false, and attach an onchange event to modifiedon where we can re-disable the field.

This works when the Save is successful, but not when the Save is cancelled because of a server error. In our case, we added a validation plugin that throws an error if certain conditions are not met. Unfortunately, we have not found a way to re-disable the field when the Save operation is cancelled due to the plugin since the built-in Save event does not have callback functions for onSuccess or onError.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Maintaining disabled field state when saving and when save is cancelled by a plugin

    You should be able to just run setSubmitMode('always') only when you update the disabled field (inside your onchange event), that way the field won't be set to submit always when the field isn't changing.

  • Community Member Profile Picture
    on at
    RE: Maintaining disabled field state when saving and when save is cancelled by a plugin

    How would I set it back to "dirty" after the save event? The problem I keep bumping into is that the form's Save event doesn't have any callbacks when it completes. I'm beginning to think that I should create my own Save button and hook up the Xrm.Page.data.save() function since it provides callbacks, but I don't think that would prevent users from saving using the little save icon on the lower right of the form.

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Maintaining disabled field state when saving and when save is cancelled by a plugin

    So the issue is then if they save the form and then do a subsequent save then it would trigger a plugin even though that disabled field didn't change?

    If I'm understanding it correctly then I think you could attach an onsave event that checks if the disabled field is dirty (msdn.microsoft.com/.../gg334409.aspx) and if it is then set the submit mode to always, otherwise set the submit mode to none.

  • Community Member Profile Picture
    on at
    RE: Maintaining disabled field state when saving and when save is cancelled by a plugin

    I didn't forget about this: it sounds like that will work. I will mark it as an answer once I test it out.

  • Community Member Profile Picture
    on at
    RE: Maintaining disabled field state when saving and when save is cancelled by a plugin

    Your solution works. Here is the javascript I put in my form's save event:

        var attribute = Xrm.Page.getAttribute("myAttribute");
        if (attribute.getIsDirty()) {
            attribute.setSubmitMode("always");
        } else {
            attribute.setSubmitMode("dirty");
        }

    This allows its value to be committed when it is dirty and disabled, while not committing the value when it hasn't changed.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans