Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Override protected new() method in class extension

(0) ShareShare
ReportReport
Posted on by 163

I made a class extension and I want to override new() method in class extension but when I do it I get an error (An extension class can only have a parameterless constructor.) so How Can I access this ?

  • Martin Dráb Profile Picture
    Martin Dráb 230,966 Most Valuable Professional on at
    RE: Override protected new() method in class extension

    You indeed seem to calling the whole logic once more, despite the fact that it's not what you original asked for. And now you seem to be confirming that this new design of yours is wrong.

  • Menna Allah Ahmed Profile Picture
    Menna Allah Ahmed 163 on at
    RE: Override protected new() method in class extension

    Now It works well , I add a new method take Journal from main method and pass it to my table buffer .

    but now 2 forms opened old and new How can I fix this ?

  • Martin Dráb Profile Picture
    Martin Dráb 230,966 Most Valuable Professional on at
    RE: Override protected new() method in class extension

    As you see, you can't call the constructor from your model, because it's internal, and you would gave to provide the right parameters if calling it was possible. Let me remind you the code you shared before:

    InventJournalTable journal = InventJournalTableJumpRefHelper::getJournalFromArgs(_args);
    var helper = InventJournalTableJumpRefHelper::construct(journal);

    Nevertheless if your goal is just overriding construct(), you likely should do just that and you don't need to call it again from your code.

  • Menna Allah Ahmed Profile Picture
    Menna Allah Ahmed 163 on at
    RE: Override protected new() method in class extension

    I tried to add new () without parameters but I got an errors

    How Can I override constract() and make it add values in my table buffer in extension (inventJournal) not table buffer in base class

    Screenshot-_2800_71_2900_.png

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,966 Most Valuable Professional on at
    RE: Override protected new() method in class extension

    I meant the code cause the error. Anyway, let me comment on it in general...

    new() isn't a method. It's a constructor and constructors can't be extended. You can create a constructor for an extension, but it can't have parameters. It seems that you tried to use parameters. You can learn more about in F&O documentation: Class extension - Method wrapping and Chain of Command.

    In your case, the right approach maybe extending construct() instead of new().

    By the way, your code has double line spacing because you pasted it in a wrong way. Always use Insert > Code (in the rich formatting view).

  • Menna Allah Ahmed Profile Picture
    Menna Allah Ahmed 163 on at
    RE: Override protected new() method in class extension

    [ExtensionOf(classStr(InventJournalTableJumpRefHelper))]

    public final class InventJournalTableJumpRefHelper_Extension

    {

      public InventJournalTable inventJournal;

       public void jumpRef()

       {

       if (!inventJournal)

           {

               throw error(Error::missingRecord(tableId2PName(tableNum(InventJournalTable))));

           }

           next jumpRef();

           var jumpArgs = new Args();

           jumpArgs.record(inventJournal);

         var menuItemName = this.getMenuItem(inventJournal);

           var menuFunction = new MenuFunction(menuitemdisplaystr(InventJournalTableProjectCFM), MenuItemType::Display);

           menuFunction.run();

       }

      // // //<summary>

      // // //Opens the target form when the user clicks on journal ID.

      // // //</summary>

      // // //<param name = "_args">Lookup arguments.</param>

       public static void main(Args _args)

       {

           next main(_args);

           if (!InventJournalTableJumpRefHelper::isArgsValid(_args))

           {

               throw error(Error::wrongUseOfFunction(funcName()));

           }

           InventJournalTable journal = InventJournalTableJumpRefHelper::getJournalFromArgs(_args);

           var helper = InventJournalTableJumpRefHelper::construct(journal);

           helper.jumpRef();

       }

      // // //<summary>

      // // //Checks if the lookup arguments are valid.

      // // //</summary>

      // // //<param name = "_args">Lookup arguments.</param>

      // // //<returns>true if the lookup arguments are valid; otherwise, false.</returns>

       protected static boolean isArgsValid(Args _args)

       {

           boolean valid = next isArgsValid(_args);

           return true;

       }

      //  //<summary>

      //  //Checks if the lookup arguments are valid.

      //  //</summary>

      //  //<param name = "_args">Lookup arguments.</param>

      //  //<returns>true if the lookup arguments are valid; otherwise, false.</returns>

       protected static boolean isArgsValidCFM(Args _args)

       {

           return _args.record().TableId == tableNum(WMSJournalTable)

                   || _args.record().TableId == tableNum(PurchLine)

                   || (_args.lookupTable() == tableNum(InventJournalTable)

                   && _args.lookupField() == fieldNum(InventJournalTable, JournalId));

       }

       public str getMenuItem(InventJournalTable _inventJournal)

       {

           str result ;

           var journalStatic = JournalStatic::newTable(_inventJournal);

           if (_inventJournal.JournalType == InventJournalType::project)

           {

               result = menuItemDisplayStr(InventJournalTableProjectCFM);

           }

           else

           {

               result = journalStatic.menuItemStrLines();

           }

           if (result == '')

           {

               throw error(Error::wrongUseOfFunction(funcName()));

           }

           return result;

       }

    }

    this is my code so I want to override new() to make inventJournal has all values that returns in main mthod

  • Martin Dráb Profile Picture
    Martin Dráb 230,966 Most Valuable Professional on at
    RE: Override protected new() method in class extension

    Can you show us your code, please?

    By the way, I moved your thread from Dynamics AX Forum to Dynamics 365 Finance Forum.

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,187 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,966 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans