Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

event handler methods

(2) ShareShare
ReportReport
Posted on by 1,535
Hi,

What is the difference between going to the table events node and copying the event handler (modified/modifying)
 
and between going to the table method, and copy it's event handler (pre/post)



 
Categories:
  • Martin Dráb Profile Picture
    233,352 Most Valuable Professional on at
    event handler methods
    Of course that you can put code to your extension, including creating a transaction. I'm sorry that you got confused and focus on the transaction while it's not really the point.
     
    The difference (between modeled events and CoC) is when the code gets executed, which is independ on what code is inside the extended method. That I used ttsbegin/ttscommit in my example was just a coincidence. There could be infolog messages instead, or any other code.
  • .. Profile Picture
    1,535 on at
    event handler methods
    Hi @Martin Dráb,



    yes i understood this point (thank you)



    But regarding transaction, if we want to use CoC maybe we could still make the code work inside transaction by doing this code below (adding another ttsbegin and ttscommint). I know it's not exactly the same, but it will guarantee that our code will be executed with the super code just like if we used table event handler onInserted-OnInserting
     
    [ExtensionOf(classStr(CustTable))]
    final class CustTable_Extension
    {
        public void insert() 
        {
           ttsbegin;
       
           //add logic if needed
           next();
    
           //add logic if needed
           ttscommit;
        }
    }



     
  • Martin Dráb Profile Picture
    233,352 Most Valuable Professional on at
    event handler methods
    Yes, you've repeated my statements correctly. Just remember that when we talk about the transaction, we're talking about the specific code I added to insert() method. If you want a generic statement, you can say things like "code in CoC above next (or in a pre-method event handler) is called before any code of the extended method".
     
    Note that you can easily try these things by yourself. 
  • .. Profile Picture
    1,535 on at
    event handler methods
    Hi @Martin Dráb,

    Just to make sure i understood correctly.

    So"CoC" and "pre/post" methods can be used for any method, but "table events" are specific for certain events happening

    And for your example about "insert":

    if I use "pre/post" or "CoC" methods, then they will be called outside the transaction?

    but if i use "table events", then it will be called inside the transaction?
  • Suggested answer
    Martin Dráb Profile Picture
    233,352 Most Valuable Professional on at
    event handler methods
    Don't look for a situation when one works and the other doesn't - that's not the difference. When modifiedField() is called (from any place), it both executes its extensions and raises the event.
     
    An important thing to realize that method extensions (using either CoC or the old pre-/post-method events) is an approach applicable to all methods (that aren't private or so); it's not specific to table events such as onModifiedField. For example, you can extend CustTable.paymName() in the same way, although there is no modeled event corresponding to the method. 
     
    You're talking about a special case when there is no just a method you can extend, but a modeled event too for the same purpose. Then you can use either. But there are differences. For example, imagine this method:
    public void insert()
    {
        ttsbegin;
    
        super();
    
        this.insertRelatedTable();
    
        ttscommit;
    }
     
    If you subscribe to a table event (onInserting or onInserted), your event handler will be called by super(), i.e. inside the database transaction. If you extend the method, your code will be executed either before all the code or after it, which means that it'll be outside of the transaction. As you see, the fact that modeled events and method extensions are called at different times may make a big difference or it may be irrelevant, depending on code inside the method.
  • .. Profile Picture
    1,535 on at
    event handler methods
    Hi Martin/Raj,

    I'm afraid i didn't get the difference exactly. So currently if i write code in both methods for example when a certain field is modified, then print sth, then both will work. And both will work if change those fields form the form, then still it will print.

    Do you mean that maybe if i write runnable class, and call .ModifiedField(), then only the event handler will work and not the table method event handler?



    And yes i know that CoCs are better, but i wanted to understand the difference between these two
  • Martin Dráb Profile Picture
    233,352 Most Valuable Professional on at
    event handler methods
    Raj's answer is correct, but note that you shouldn't normally use the method event handlers. Chain of command (CoC) is a successor of this approach and it offers bother easier and safer way to extend methods.
     
    The chat bot (Holly) missed the point of the question.
  • Suggested answer
    Raj Borad Profile Picture
    1,091 on at
    event handler methods
    Hi,
     
    In D365 FO, copying an event handler from the Table Events node (e.g., onModified, onModifying) hooks into data-level events that occur automatically when a record is modified through the data access layer—such as via forms or services—regardless of custom method logic. These are tied to database changes.
     
    In contrast, copying an event handler from a Table Method (e.g., insert, update, validateWrite) targets specific code execution logic and lets you hook into pre/post-method behavior. This is useful when you want to add logic before or after a method runs, including custom business logic.
     
    This is the general description. What are your scenarios where you want to apply it, or you are just here for the understanding? 
  • Suggested answer
    Holly Huffman Profile Picture
    6,078 on at
    event handler methods
    Good morning, afternoon, or evening depending on your location!
     
    The difference between the Modified and Modifying event handlers lies in when they are triggered during the lifecycle of a field change in Dynamics 365:
    1. Modified Event Handler:
      • This is a post-event handler, meaning it is triggered after the field value has been changed.
      • Use this when you want to execute logic that depends on the new value of the field being already set.
      • Example: Logging the updated value or triggering downstream processes.
    2. Modifying Event Handler:
      • This is a pre-event handler, meaning it is triggered before the field value is changed.
      • Use this when you want to validate or modify the value before it is committed to the database.
      • Example: Preventing invalid data from being entered or applying transformations to the input.
    Practical Use Case:
    • If you want to validate input or restrict changes, use the Modifying event.
    • If you want to react to a change or trigger additional actions, use the Modified event.
    When you right-click on the event in the table's Events node and select "Copy Event Handler Method," you can choose between Pre-Event Handler (for Modifying) and Post-Event Handler (for Modified). This allows you to tailor your logic to the specific timing of the event.
     
     
    Hope this helps some!

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 243

#2
Martin Dráb Profile Picture

Martin Dráb 190 Most Valuable Professional

#3
Adis Profile Picture

Adis 144 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans