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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

After save callback

(0) ShareShare
ReportReport
Posted on by 607

Hi

we are trying the following:

when changing some values and hit save, the fields should be disabled after the save. If I call the function to disable the fields in OnSave, the save happens after disabling the fields. This does not work, because the value of these fields is not being saved. When using setSubmitMode, all these fields are part of the save context, this makes plugins and workflows on this entity trigger unwanted.

I've tried the AddOnSave function, but again the save happens after this callback.

Calling the Xrm.Page.data.entity.save().then(successCallback, errorCallback) from OnSave, did not work either, because this creates a loop.

Does anyone have other suggestions?

Grtz

*This post is locked for comments

I have the same question (0)
  • Bojan.borisovski@hotmail.com Profile Picture
    480 on at

    HI,

    i have not had the success of using the  Xrm.Page.data.entity.save().then(successCallback, errorCallback), successfully either. For me the successCallback, was executing before the save was made, and record was changed in the db. My guess is that something is not right in CRM with this callback functions logic.

    What i would suggest is, if you are using CRM 2013 and and you do not need to query the db for disabling the fields, is to use the Business Rules option, to achieve disabling of fields, based on other field values.

    Here is a link for more info:

    msdynamicscrmblog.wordpress.com/.../business-rules-in-dynamics-crm-2013

    Hope it helps.

    Bojan,

  • DlwK Profile Picture
    607 on at

    Hi Bojan

    Thanks for your reply.

    In fact, using business rules gives me the same problem. If I set some attributes disabled according to a certain value, then they are not part of the save, because they do not have the correct submit mode. When disabling a field, its value is not saved by default.

    This brings me back to the same problem?

    Or I set submit mode to always, and plugins might get triggered when I don't want them to, or I have to disable them after the save, which seems impossible so far.

    Grtz

  • Bojan.borisovski@hotmail.com Profile Picture
    480 on at

    In such a case maybe it would be better, to update fields you need directly with JavaScript, and just refresh the page.

    Can you give some specific code, and more detailed logic and process on what you are trying to achieve, so i can get better understanding?

    Thanks

    Bojan.

  • DlwK Profile Picture
    607 on at

    Hi Bojan

    if the form loads we call this procedure:

    function lockFieldsOnStatusSubmitted()

    {    

    var state = Xrm.Page.getAttribute("statecode").getValue();

    var status = Xrm.Page.getAttribute("statuscode").getValue();

    var locked = (state == 0 && status == 751330001);

    Xrm.Page.getControl("statuscode").setDisabled(locked);

           Xrm.Page.getControl("new_ratingid").setDisabled(locked);

    }

    Therefore, in the form I call the function onFormLoadCustom, which calls lockFieldsOnStatusSubmitted.

    I've tried to call lockFieldsOnStatusSubmitted()  from OnSave, by calling onFormSaveCustom from the form (onSave). This locking happens too soon. I've also tried to add it as a handle by calling Xrm.Page.data.entity.addOnSave(lockFieldsOnStatusSubmitted);. This is done from onFormLoadCustom. Again, locking happens too soon.

    I also tried to call the save method myself, from onFormSaveCustom, but this results in a loop.

    In CRM 2011, a save made the form to reload, making the onLoad triggers execute again. But this is not the case anymore. I am looking for a save callback, or a moment to reload the data. Is there an event on the form where data is reloaded?

    Hope this is enough, I don't have much code to show to you.

  • Bojan.borisovski@hotmail.com Profile Picture
    480 on at

    Yes, quite understandable.

    Do you manually update the status field?

    As a workaround you can try the following

    Leave the event onLoad as in CRM 2011 and

    Try to reload whole window in the save callback function with the following:

    1. Open the same record again with:

    Xrm.Utility.openEntityForm("entityname", "entityid", null);

    or

    2. Try to reload whole window with

    location.reload();

    It will be ugly on the new UI, but you can try.

  • Verified answer
    Linn Zaw Win Profile Picture
    3,407 on at

    Check whether the attribute value is changed or not with getIsDirty before setting setSubmitMode so that unnecessary plugins and workflows won't be triggered.

    if (Xrm.Page.getAttribute("new_attribute").getIsDirty())

    {

       Xrm.Page.getAttribute("new_attribute").setSubmitMode("always");

    }

  • DlwK Profile Picture
    607 on at

    Hi

    I already tried the location.reload() in OnSave. This works, but I don't understand why. Locking the fields from this moment happens before the save, but a reload first saves the values and then reloads the window? Or is all of this happening async and can't we be sure of the moment of execution?

    I guess the response of Linn Zaw Win will also work , I still have to try this one as well. Maybe it is a more pragmatic solution to the problem.

    I'll post the results for this one as well

  • Bojan.borisovski@hotmail.com Profile Picture
    480 on at

    The biggest problem with Save in CRM 2013 is that it doesn't reload the window, and doesn't fire on-load after save. When you initiate location.reload() the whole page is refreshed and onload of the form is initiated.

    But you have to be careful with this, if you register the refresh after save, due to the autosave functionality of CRM 2013, your page will be constantly reloaded, so you have to add a check there, for the reload of the page to happen only when the status has changed to a certain value.

  • DlwK Profile Picture
    607 on at

    Hi Bojan

    thanks for your quick reply. Indeed, autosave is turned off for all forms for this project.

    I do understand that we initiate a reload our selves, but I still don't quite get why this reload works after save and my field locking function before the save.

    Thanks

  • DlwK Profile Picture
    607 on at

    The solution with the getIsDirty works also, and looks a bit cleaner.

    Does getIsDirty always work, I mean do you have to leave the field before an attribute gets dirty?

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans