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 :
Finance | Project Operations, Human Resources, ...
Answered

What is the best method to update source data during Even Handler

(0) ShareShare
ReportReport
Posted on by 596

Hi guys,

I need some advice on how to update source data during Even Handler action ? The explanation is like this :

  1. I have a form, lets say Sales Order
  2. I created an On modified Event Handler on some of its field (lets say Delivery date), This Event Handler will activate a batch job doing some routine, and in the end it will need to update back this Sales Table in this Sales Order data source to acknowledge that the action is done. It need update some field in Sales Table, lets just say ActionId and Action Date.

The thing is, Sales Order form, is still in Edit mode, right? When the event handler update back Sales Table, it success. But if in Sales Order form, it will have a message saying, "Cannot edit record. The record shown has been updated by another user Press revert or...…"

Understand the cause of it, only don't know how to handle this such case ?

Kindly advice.

Thanks

I have the same question (0)
  • Verified answer
    WillWU Profile Picture
    22,361 on at

    Hi Voltes,

    You can call reread() method to update the datasource form cache with the values from the database , and then call refresh() to redraw the grid and show the changes.

    formDataSource.reread()

    formDataSource.refresh()

    Hope this helps.

  • Voltes Profile Picture
    596 on at

    Hi Will,

    Thanks, I just remembered this, but still I missed the Reread first. Added in the Event handler class :

    sender.datasource().Reread();

    sender.daasource().Refresh()

    So basically it always need these 2 steps, right? cannot just refresh.

    Many thanks!

  • Voltes Profile Picture
    596 on at

    Hi Will,

    A second thought, I found out this Reread is not updating the field changes of the Event Handler it self.

    So for example, the event handler is for Delivery Date, I changed to tomorrow's date, and in the event hander code I added those two lines (after all the updates done and ttscommit) the Delivery date will stay as it is (before changes), but the new field which I want to update, did updated and showing.

    Thanks

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Actually it's not best practice to update the record after user modified some field. The user should always be able to cancel what they were doing by pressing esc. But if your code performed update on that record, those changes would not be rolled back.

    So you should not trigger your code when the field is modified, but only after the delivery date is really updated in the table (=in update method).

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Or, you can also hook in Classes\SalesTableType\modifiedField to change other fields based on the field that the user changed, without yet saving the record.

  • Voltes Profile Picture
    596 on at

    Noted. I think I do agree that it should be updated after all the changes saved. And yes it is better to use in Update method.

    But just my curiosity, I'm using this event handler actually because I thought this is the new way of this D365 suggesting. But I've come to found out that a lot of things still better to do it in the old ways. So what is actually the usage of this Event Handler after all.

    Thanks.

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Event handlers (and Chain of Command, which is better and recommended whenever you can) are just ways of adding your code to be executed before/after standard methods. Where to place your code and how to write it are another story, and the same principles apply than in AX2012.

    For example, in AX2012 you could write your code to modified() method of a form control. Now you can't but you can use onModified event. Whether you should or should not put your code in modified method/event handler, or update method/event handler is not different between AX2012 and D365.

  • Voltes Profile Picture
    596 on at

    Thanks so much for the explanation.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 540 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans