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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Event handlers and CoC exactly when to choose

(0) ShareShare
ReportReport
Posted on by 332
Hi team,
 
I know about the behaviour of "Event handlers" and "CoC". But confuse about the selection . I mean when to think about to select CoC or Event handlers.
 
Kindly elaborate pls.
 
Thanks!
Categories:
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    239,795 Most Valuable Professional on at
    Event handlers are method executed when an event is triggered, e.g. a record is being inserted or a form control value was modified.
    CoC is used to extend types and methods.
    You typically can't use both, e.g. because you want to extends a method that doesn't have a corresponding event you could subscribe an handler method to, or you want to add a an instance variable to a class, which has nothing to do with event handlers.
    In some case, you have a choice, e.g. you can either create a CoC extension of insert() method or use an event handler of Inserting/Inserted event. Sometimes you can use both, but you must understand that they're executed at different times, which may make a bid difference. For example, there may be code in insert() with a DB transaction. Events get raised in super(), in the transaction, but if you use CoC, you're outside the transaction (and you may want to create another one on your own.
     
    There is also a predecessor of CoC that allows you to subscribe to special events before and after method calls, but they're rarely useful. CoC is both easier to write and read, and safer.
  • CU10121822-0 Profile Picture
    332 on at
    Hi Martin, thanks for your reply.
     
    As you said " in some cases we have choice , either go for Table - insert method or Event handlers of inserting/inserted.
     
    So, in such kind of scenario, we can either go for CoC or Event handlers. Because both are similar just the way of calling is different. If I am not wrong.
     
     In other scenario, like button clicked method we should go for event handlers. Because when click event happened, the method got executed.
     
    Am I correct?  Kindly verify please.  thanks!
     
  • Martin Dráb Profile Picture
    239,795 Most Valuable Professional on at
    Yes, I explained that sometimes you have a choice, but also that the options aren't identical and you may either need a particular one anyway, or you may need different logic depending on what you use.
     
    A button click is another example where you have a choice - there is both an event and the clicked() method. Note that you typically want to extend the logic called by the button (e.g. a class called by a menu item button), not the button itself.
     

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 503 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 487 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 433

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans