Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Access elements declared in form scope from class extension

Posted on by 3,208

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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Access elements declared in form scope from class extension

    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;

       }

    }

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 228,089 Super User 2024 Season 2 on at
    RE: Access elements declared in form scope from class extension

    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.

  • Zain Bokhari Profile Picture
    Zain Bokhari 3,208 on at
    RE: Access elements declared in form scope from class extension

    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
    Martin Dráb 228,089 Super User 2024 Season 2 on at
    RE: New Methods on Form Extension without Over-Layering. Is there a way to access Elements declared in form scope?

    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
    Zain Bokhari 3,208 on at
    RE: New Methods on Form Extension without Over-Layering. Is there a way to access Elements declared in form scope?

    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?

  • Martin Dráb Profile Picture
    Martin Dráb 228,089 Super User 2024 Season 2 on at
    RE: New Methods on Form Extension without Over-Layering. Is there a way to access Elements declared in form scope?

    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?

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,252 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,089 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans