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

Which one is recommended to use mostly CoC or Event handlers

(4) ShareShare
ReportReport
Posted on by 438
Dear team,
 
As I go through about CoC and Event handlers.
So, found its ack as like Pre and Post activity.
 
1. Could you please let me know, which one is recommended mostly to use CoC or Event handler.
 
2. Where I will decide to go CoC to use .
 
3. Where I will decide to go Event handlers.
 
Kindly explain pls.
 
Thanks in advance.
 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    235,999 Most Valuable Professional on at
    Which one is recommended to use mostly CoC or Event handlers
    Don't you think that my first reply should be marked as verified too? When talking about why you should prefer CoC, even the verified answer refers to that.
  • Martin Dráb Profile Picture
    235,999 Most Valuable Professional on at
    Which one is recommended to use mostly CoC or Event handlers
    Note that events modeled in AOT aren't just related to DB changes, but also form events such as when a form is being initialized or a form control value has changed. 
  • Verified answer
    Layan Jwei Profile Picture
    8,008 Super User 2025 Season 2 on at
    Which one is recommended to use mostly CoC or Event handlers
    Hi,

    To answer your question, CoC is better.

    But please note, there are three types:

    CoC (you can use it on tables, forms, form datasources, form datasource field, form control, classes) 
    It lets you add logic before or after a method runs (the method doesn't have to be related to a table event like insert, it could be any method) 
    so it gets called before/after any logic of the method we are extending
    So if the original method was like the code below, then when you use CoC, your code will  be either called before  the "Hi" or after the "bye" depends if you put the code before next or after it
    public void insert()
    {
            Info("Hi");
            super();
            Info("Bye");
    }

     
    Pre/post event handler methods (you can find them on table/form methods, classes)
    Exactly same as CoC, It lets you add logic before or after a method runs (the method doesn't have to be related to a table event like insert, it could be any method) 
    so it gets called before/after any logic of the method we are extending
    So again. it's just like CoC but CoC is better for the reasons Martin mentioned in the first reply (for me i like CoC because it's easier, no hardcoding and i feel it's more organized)
    And maybe another reason that i noticed, is that CoC can be written for form datasources, form datasource fields and form controls, but i couldn't see pre/post for sth other than tables/forms/classes (i mean if u right click on form data source method, you won't see pre/post)


    The third one is different:
    Event handlers ( you can find them on tables, forms, form datasources, form datasource fields, formControls and classes)
    Example: OnInserted/OnInserting, they occur automatically whenever a record is inserted
    Those event handlers are related to database changes, so it doesn't work on any method like CoC or pre/post event handlers.
    The event handler will be called by the super() itself and not before/after the method runs
    There could be cases where you might want to use those event handlers instead of CoC as they have benefits too. For me if the logic would work using both with no differences, then i would go with CoC always  (i think this not the scope for your question so i won't go further)


    So if the original method was like below, when you use table event handles, your code will be called inside the super (OnInserting/OnInserted) and not before Hi/bye as CoC or pre/post methods
    public void insert()
    {
            Info("Hi");
            super();
            Info("Bye");
    }
     

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future.

  • Martin Dráb Profile Picture
    235,999 Most Valuable Professional on at
    Which one is recommended to use mostly CoC or Event handlers
    I'm confused too. Please be more specific.
     
    Do you mean that you know neither concepts you asked about (CoC and pre-/post-method events) and you want to show us examples, even of the one that you shouldn't use?
     
    Or do you mean that you want example of the pre-/post-method handlers (that you shouldn't use) and the modeled events unrelated to your question?
     
    In my opinion, the answer to your question is that you should use CoC, and I thought you knew what it was.
  • CU05031448-0 Profile Picture
    438 on at
    Which one is recommended to use mostly CoC or Event handlers
    Really confused.
     
    Can anyone please give me simple example for each. So, that I can understand both.
     
    Thanks in advance 
  • Martin Dráb Profile Picture
    235,999 Most Valuable Professional on at
    Which one is recommended to use mostly CoC or Event handlers
    Note that Syed's answer about a different type of events, therefore it's not really relevant to this discussion.
  • Syed Amir Ali Profile Picture
    125 on at
    Which one is recommended to use mostly CoC or Event handlers
    Hey, 

    When to Use CoC (Chain of Command)
    1. You want to modify the behavior of existing methods in a class/table/form without changing the base code.
    2. You need to access protected members or methods of the original class.
    3. You want to call the original (base) method using next.

    When to Use Event Handlers
    1. You want to respond to a specific event (e.g., insert, modify, validate).
    2. You are writing non-intrusive logic that should not change the base behavior.
    3. You want to attach logic dynamically without extending the class itself.
     
  • Suggested answer
    Martin Dráb Profile Picture
    235,999 Most Valuable Professional on at
    Which one is recommended to use mostly CoC or Event handlers
    Event handlers have a much broader use, but if you're talking about those pre-/post-method event handlers, don't use them at all. CoC can do the same in a much easier and safer way. For example, with CoC, you simply refer to method parameters by name and the compiler checks the names and types (therefore you get an error if the original method parameter change). With pre-/post method handlers, you need to refer to parameters either by their names as strings, or by their index, without any compile-type controls. It's more work, uglier code and it dangerous, because the compiler can't spot any errors in that. You also can't access protected members in these handlers.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 841 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 566

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans