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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Get caller name in Table method event handlers

(0) ShareShare
ReportReport
Posted on by 1,244

Hi,

Greetings!

I have created an event "PostHandlerFor" on a standard table's method. The method is called by multiple standard classes.

I want the code of the event to execute only if called by a particular class.

Is there a way to get the name of the caller?

Regards,

Abhinay

I have the same question (0)
  • Pedro Tornich Profile Picture
    955 on at

    Hi Abhinay,

    Event handlers have no caller, they are called by the framework before or after the actual method.

    If you were in the original method, how would you know the caller? Is there a parameter to identify the caller in the original method?

    If the caller can be identified by a parameter, then in your event handler you can access that parameter using:

    _args.getArg(identifierStr(variableName))

  • syed baber Profile Picture
    11,633 on at

    Hi Abhinay,

    Which table's method you are trying to write the post handler for ? What you can do is to create a new field on the table and assign some value to it before calling and then inside the table's method, identify the caller based on that value. Hope this makes sense.

    Let me know if you have further queries.

  • nmaenpaa Profile Picture
    101,166 Moderator on at

    As mentioned by others, you can't access the caller if the table method doesn't know it. The event handler can't know more than the method where you hooked it.

    Perhaps you should hook your event handler in the caller class, not the table? Could you share more details about what you are trying to achieve? Thanks!

  • Suggested answer
    Abhinay Tharwal Profile Picture
    1,244 on at

    Hi Pedro,

    Thank you for your suggestion!

    [quote user="Pedro Tornich"]

    If the caller can be identified by a parameter, then in your event handler you can access that parameter using:

    _args.getArg(identifierStr(variableName))

    [/quote]

    I used xSession::xppCallStack() and from there I got the list of classes.

    It required less customization/ changes to objects and got the job done.

    Regards,

    Abhinay

  • Abhinay Tharwal Profile Picture
    1,244 on at

    Hi Babar,

    Thank you for your suggestion!

    [quote user="syed baber"]What you can do is to create a new field on the table and assign some value to it before calling and then inside the table's method, identify the caller based on that value. Hope this makes sense.[/quote]

    Since it is a standard table I wanted to avoid modifying it as much as possible. Please refer to my earlier reply to Pedro for my approach.

    Regards,

    Abhinay

  • Abhinay Tharwal Profile Picture
    1,244 on at

    Hi Nikolas,

    Thank you for your suggestion.

    [quote]Perhaps you should hook your event handler in the caller class, not the table? Could you share more details about what you are trying to achieve?[/quote]

    The calling classes are many (and part of standard code) and I wanted to avoid it.

    Please refer to my reply to Pedro's suggestion for my approach.

    Regards,

    Abhinay

  • Martin Dráb Profile Picture
    239,029 Most Valuable Professional on at

    I strongly recommend looking for a different design.

    If you base your business logic on implementation details such as the name of a class or a position in the call stack, any otherwise save refactoring might break your system. You also make tight dependency of the called code on its caller (in addition to the dependency in the other direction), making further obstructions to maintainability, testability and extensibility.

    If no better solution is possible, I would use a thread-level variable describing the context. Although global data is not optimal, this - unlike your solution - allows an intent-revealing design which doesn't depend on particular names or call stack, there is no tight dependency to the caller and the content can be set as needed from anywhere, including unit test classes.

  • Abhinay Tharwal Profile Picture
    1,244 on at

    Hi Martin,

    Thank you for your suggestion, I appreciate it!

    I will look-into it and try to  come up with a way.

    [quote]I would use a thread-level variable describing the context.[/quote]

    Could you please help me with some links/ material to refer to?

    I will update this thread as soon as I have something to add here.

    Regards,

    Abhinay

  • Pedro Tornich Profile Picture
    955 on at

    Hi Abhinay,

    You used the call stack to identify the caller, but as Martin said, it's a bad design.

    In my opinion the best alternative is to identify all methods that calls the method you are trying to extend, which I believe you already know since you would need a switch/case to act upon the call stack information.

    After you discover all possible callers you can write a post event handler for each one. You can have a static method to centralize the business logic and then you call it in all event handlers. You can have the static method and the event handlers in the same class for the sake of simplicity.

    This is more work indeed, but you will thank yourself if you ever need to maintain this code.

  • Suggested answer
    Sukrut Parab Profile Picture
    71,735 Moderator on at

    Hi Abhinay ,

    You can take a look at  below link . Take a look at option 3

    Option 3: Disposable context

    Scenario

    You need to pass extra information from your code to a pre/post handler somewhere downstream.

    community.dynamics.com/.../extensible-x-method-signatures

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

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

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans