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 AX (Archived)
Answered

When to use event handler in AX?

(0) ShareShare
ReportReport
Posted on by

Hi Experts,

What is the basic use of event handlers? What are the advantages of them over writing code directly to methods? (except upgrade advantages)

I have gone through few blogs but, They were more about How to use event handlers not when or why to use them.

Thanks in Advance:).

*This post is locked for comments

I have the same question (0)
  • Verified answer
    dolee Profile Picture
    11,279 on at

    Hi,

    The "upgrade" part is the main advantage I'd say. Besides, with event handlers (pre, post) then it is possible for multiple models to edit the same method (in a sense) in the same layer.

    Other than that, it's more personal preferences. I myself like to use them when possible, they are:

    - Easier to read.

    - Saves me from a lot of scrolling when the method itself has a lot of lines.

    - It feels more organized. For example, if I write multiple event handlers to a table, I'll put them into one class that is specific for that table.

    As long as you can live with the small overhead and the fact that we can't guarantee which event handlers trigger first when there's multiples, I'd recommend using it.

  • Community Member Profile Picture
    on at

    Hi,

    Thanks for the quick response. I agree with you on their use for standard tables. How about using them for methods in new tables created for customization? Since there is no upgrade advantage here. Which one we can consider as best practice (using Event Handler/ writing in method itself)?

    Thanks,

    Prashanth N N

  • dolee Profile Picture
    11,279 on at

    I don't represent the best practice. Having said that, for new tables I'd stick with writing in the method itself until I find a reason to mix in event handlers.

  • Verified answer
    Martin Dráb Profile Picture
    237,876 Most Valuable Professional on at

    Decoupling classes is very important for good architecture. For example, why should a table have a direct reference to five different classes running some logic when a record gets inserted? Why would you have to update the table if you remove one of these classes or add an additional one? What if some of these classes exist in modules that don't have to be installed in all environments? By using events, you decouple the table raising the event and classes that react to the event. The table won't know or care about how many classes subscribed to the event and won't be impacted by changes in the logic. You can install and uninstall modules as you like and everything will work without any extra work.

    You'll see it makes much better sense in AX 7, where you could put the table to a completely different package than event handler classes. The situation in AX 2012 is worse and I wouldn't use events excessively there, but be prepared that they're crucial in AX 7. When writing new code in AX 2012, you should already consider how you'll upgrade it to AX 7 later.

    Also, upgrade conflicts can occur in your code as well, if somebody can override the method in a higher layer. For example, it's quite common that partners build a solution in VAR layer which is further customized for a specific customer in CUS layer.

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 AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans