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)

How to Get Form level methods and fields in events

(0) ShareShare
ReportReport
Posted on by

Hi all,

I am working in D365 with Extensions way of coding. I want to access form level methods and variables in events.

e.g. In SalesTable class declaration there are a few variables declared. like, 'mcrLastSourceId'. I want to access this variable in any event handler, say, MyButton_Clicked. so how can I get this variable.

Same for methods, I want to call a method e.g. 'mcrSetScriptStr' in an event. so how to call it.?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    If they're private, you can access them only from the class where they're defined, not from other classes (including the class where you have your event handlers).

  • Community Member Profile Picture
    on at

    Private is not mentioned explicitly, so I believe default scope is protected (Correct me if i am wrong).

    So can we use these protected variables in Event handlers?

  • Suggested answer
    Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    Hi Shahrukh,

    You would need to analyse for which purpose these variables and methods are used in your existing AX system. They must have been used under some event/action e.g. when form opens it will call few methods (could be custom ones) these variables have used there. Check that call of the method and create new event in new created extension class.

    Below blog article is just to give you an example and it does not fit with your requirements so don't follow everything from here. Here a button from form is being called in class and enable/disable it.

    devserra.wordpress.com/.../ax7-access-form-extension-controls-via-event-handler

  • Suggested answer
    Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    Protected members can be only used in child classes, which likely isn't your case either. Don't try to find some magic in it, all rules of the language still applies here as in other contexts.

    That being said, it's worth to note that Microsoft consider breaking the protection level of protected members and makes them available to class extensions. It's a violation of OOP rules, but because most of the codebase isn't designed to be extensible, this would be a workaround to allow extensions to be used in more places.

  • Faisal Fareed Profile Picture
    10,796 User Group Leader on at

    Martin very well explained, MS has to do this otherwise many of the functionalities will not be extensible in AX.

  • Community Member Profile Picture
    on at

    So it means we can't access private or protected methods or variables of a form.

    But can we access form level public methods or variables in events ? what is the syntax to access public methods and variables.

  • Suggested answer
    Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    You should be familiar with calling public methods. The syntax is objectName.methodName(parameters) for instance methods and ClassName::methodName(parameters) for static methods. Forms are a bit trickier, as they aren't types, so if you want to access custom methods (not those defined on FormRun), you either do it through an interface, if the form implement some (which isn't likely), or you have to resort to late binding (similarly to what was done in all previous versions of AX).

    Public variables can be accessed in a similar way, such as objectName.variableName, but they shouldn't be used at all, because it breaks encapsulation. If you want to expose a variable, you should do it through a method.

  • Community Member Profile Picture
    on at

    Martin,

    You have explained very well but my scenario is a bit different.

    I have created an extension of salesTable and added a new data source in that extension. Now I have written an event handler OnPostLinkActive. And i want to call a method that is defined on SalesTable form, in my event handler. So how can i access that. The link you have mentioned is talking about pre-post event handlers. but I simply want to call a method in my event handler. 

  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    That's what my reply is about. Don't throw away the whole reply just because one example in a link about one of discussed topics uses a different type of event than you do in your code. That's not the point at all.

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
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans