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)

Access elements declared in form scope from class extension

(0) ShareShare
ReportReport
Posted on by 3,208 Moderator

Hi everyone,

I have worked with extensions on other elements but I am unable to find a proper way to add new methods to an extension class for a form in such a manner that form controls and elements are also accessible. I really want to avoid customization/over-Layering. Anyone??

Thanks.

*This post is locked for comments

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

    It seems that you're already aware of class extensions and your problem lies in what you described as "such a manner that form controls and elements are also accessible". Can you please explain what exactly you mean by that?

  • Zain Bokhari Profile Picture
    3,208 Moderator on at

    Hi Martin,

    Actually I have to add a new display method on HRMCompFixedEmplActionDialog form.

    1830.Capture.PNG

    Lets just say I have to create a replica of the default method actionId shown above.

    Now it being within the scope of the actual form can access the declared object hrmCompFixedEmplAction within form scope and similarly in other form's cases we can access the declarations, datasources, current record or form controls in a method within its scope.

    Is there a way to access any of these in my extension class method?

  • Verified answer
    Martin Dráb Profile Picture
    237,970 Most Valuable Professional on at

    You can access things like datasources, but you're trying to read a non-public member variable, which isn't allowed by its protection level. Your class extension is a separate class and doesn't have access to non-public members.

  • Zain Bokhari Profile Picture
    3,208 Moderator on at

    I know but what can we do if we need to add a new similar method like in the above example. It would need to use those variables to work correctly, can we avoid customization? Or is there a work around for achieving this?

  • Verified answer
    Martin Dráb Profile Picture
    237,970 Most Valuable Professional on at

    Although it's technically possible to bypass the protection, it would be a very bad thing to do. Violating basic design concepts of object-oriented programming, such as implementation hiding, would defeat the purpose of why these concepts were introduced (such as make them harder to break if some implementation details change). You really must respect protection levels and if some member isn't exposed by an object, you simply aren't supposed to meddle with it. Note that member variables shouldn't ever by public anyway; they can be exposed through methods.

    If you need something like that, consider if there isn't a better design that doesn't require access to protected members and if there isn't, either use a customization (if the model isn't locked) or ask Microsoft for making the object extensible.

    In your particular case, such a better design exists. Notice that hrmCompFixedEmplAction is merely another reference to element.args().caller(), therofore you can simply assign the same reference to your variable and use this variable in your methods.

  • Community Member Profile Picture
    on at

    I did something similar with the form ReqTransPO. It has a form element declared as "ReqTransFormPO          reqTransForm;" which I wanted to access from an extension class.

    I did it with the following code:

    [ExtensionOf(formstr(ReqTransPo))]

    final class OABShowOrHideProdPool_Extension

    {

       /// <summary>

       ///

       /// </summary>

       /// <param name="args"></param>

       [PostHandlerFor(formStr(ReqTransPo), formMethodStr(ReqTransPo, initDesign))]

       public static void ReqTransPo_Post_initDesign(XppPrePostArgs args)

       {

           FormRun formRun = args.getThis();

           ReqTransFormPO reqTransForm = formRun.getReqTransForm();

           if (reqTransForm.parmReqRefType() == ReqRefType::BOMPlannedOrder)

           {

               formRun.ReqPO_OABProdPoolId().visible(true);

           }

           else

           {

               formRun.ReqPO_OABProdPoolId().visible(false);

           }

       }

       public FormControl ReqPO_OABProdPoolId()

       {

           return ReqPO_OABProdPoolId;

       }

       public ReqTransFormPO getReqTransForm()

       {

           return reqTransForm;

       }

    }

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