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 :
Finance | Project Operations, Human Resources, ...
Answered

Context Menu in D365fo

(0) ShareShare
ReportReport
Posted on by 384

Dear Gents,

i want to add context menu to the available ones on inventtrans form, i am using the chain of commands to override methods on Inventtrans form, but the context menu is not showing the new added one,

please check my code below

[ExtensionOf(formStr(InventTrans))]
final class Inventtrans_Extension
{
    public const int    selectedMenu = 1;
    public  str getcontextmenuoptions()
    {
      
        ContextMenu menu = new ContextMenu();
        ContextMenuOption option = ContextMenuOption::Create("Filter By Customer",selectedMenu);

        List xx = new List(Types::Class);
        xx.addend(option);

        menu.ContextMenuOptions(xx);
        return menu.Serialize();
    }

    public  void selectedMenuOption(int selectedOption)
    {
        switch (selectedOption)
        {
            case -1:
                break;
            case selectedMenu:
                Info("Hi");
                break;
            default:
                Info("Hi");
                break;
        }
    }

}

your help is highly appreciated

regards,

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Actually there are no methods getContextMenuOptions or selectedMenuOption in FormRun class. So you are not "overriding" anything, you just added methods with those names on the form. But the D365 context menu doesn't know anything about your new methods. Also, Chain of Command never overrides anything, it adds code that is run before/after the base method.

    GetContextMenuOptions and selectedMenuOption methods are always related to a specific form control, not the FormRun. So you should put these methods in the extension class of the form control where you which you want to customize. Also, Chain of Command methods must always call next() - compiler will give you error if you don't do that.

    I'm not 100% sure that it will still work - it depends whether it's supported to hook into these framework methods with CoC or not.

  • sylvesterPowerBi Profile Picture
    384 on at

    so you suggest  to create a chain of commands  on the context method on form  ?

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Nope. I said that there is no context menu on the form level, only on different form controls.

    You can find it out easily by practicing on your own form - this way you don't yet need to concentrate on extensions, Chain of Command or such.

    So please experiment a bit on your custom form and pay attention on where you can actually override these methods.

    It's not possible on the FormRun level. But it's possible on FormControl level.

    If you want to use CoC on FormControl, then you need to create an _Extension class for the form control, not the form.

    For example [ExtensionOf(formControlStr...

  • sylvesterPowerBi Profile Picture
    384 on at
    [quote]

    Nope. I said that there is no context menu on the form level, only on different form controls.

    You can find it out easily by practicing on your own form - this way you don't yet need to concentrate on extensions, Chain of Command or such.

    So please experiment a bit on your custom form and pay attention on where you can actually override these methods.

    It's not possible on the FormRun level. But it's possible on FormControl level.

    If you want to use CoC on FormControl, then you need to create an _Extension class for the form control, not the form.

    For example [ExtensionOf(formControlStr...

    [/quote]

    my code is working on my new form, so there is no bug with the code, the only challenge here is how to involve the return and the next in one method,

    i saw different cases saying that you can write return x && next xx();

    in my case its giving an error because x is boolean and xx is string, can u help me with this ?

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    I don't understand your question.

    Your Chain of Command method must return same data type that the base method.

    And the next() call returns also the same type than the base method (it actually calls the base method).

    Could you perhaps share your code? I guess it's different than what you shared in your original question since I don't see any booleans or variables called x there.

  • sylvesterPowerBi Profile Picture
    384 on at

    here is my code

    [ExtensionOf(formControlStr(InventTrans,Inventtrans_ItemId))]
    final class Inventtrans_Extension
    {
        public const int    selectedMenu = 1;
        public  str getcontextmenuoptions()
        {
    
            ContextMenu menu = new ContextMenu();
            ContextMenuOption option = ContextMenuOption::Create("Filter By Customer",selectedMenu);
    
            List xx = new List(Types::Class);
            xx.addend(option);
    
            menu.ContextMenuOptions(xx);
            menu.Serialize();
            var x  = next getcontextmenuoptions();
            return  menu.Serialize() && x ;
           
             
        }
    
        public  void selectedMenuOption(int selectedOption)
        {
            switch (selectedOption)
            {
                case -1:
                    break;
                case selectedMenu:
                    Info("Hi");
                    break;
                default:
                    Info("Hi");
                    break;
            }
            next selectedMenuOption(selectedOption);
        }
    
    }

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    So you are returning "menu.Serialize && x"? What are you trying to achieve with that?

    Do you understand that this method must return the serielized contextMenu in order for it to work? Now you are returning a boolean. I guess you are somehow trying to return two things - the standard menu and your menu but that can't work.

    Here's what your code should do:

    1) Get the standard serialized context menu by calling next()

    2) Append your own stuff to the standard result that you got in step 1

    3) Return the new serialized context menu which now contains the standard menu with your additions

    Or, if you don't wish to return the standard menu at all, then ignore the return value of the next, and return simply menu.serialize(). Then it returns only your menu.

  • sylvesterPowerBi Profile Picture
    384 on at
    [quote

    So you are returning "menu.Serialize && x"? What are you trying to achieve with that?

    Do you understand that this method must return the serielized contextMenu in order for it to work? Now you are returning a boolean. I guess you are somehow trying to return two things - the standard menu and your menu but that can't work.

    Here's what your code should do:

    1) Get the standard serialized context menu by calling next()

    2) Append your own stuff to the standard result that you got in step 1

    3) Return the new serialized context menu which now contains the standard menu with your additions

    Or, if you don't wish to return the standard menu at all, then ignore the return value of the next, and return simply menu.serialize(). Then it returns only your menu.

    [/quote

    i dont know if i get it right, but here is what i understood from the above

    1-call next method

    2-write my own code

    3-return my own menu

    please check my code below

     public  str getcontextmenuoptions()
        {
            next getcontextmenuoptions();
            ContextMenu menu = new ContextMenu();
            ContextMenuOption option = ContextMenuOption::Create("Filter By Customer",selectedMenu);
            List xx = new List(Types::Class);
            xx.addend(option);
            menu.ContextMenuOptions(xx);
            return  menu.Serialize() ;
           
             
        }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    This replaces the standard context menu with your own context menu. If that's what you want to do, then it looks right. Of course you need to test it to find out if it's right.

  • sylvesterPowerBi Profile Picture
    384 on at

    after testing, i can still see the original menu , it's like it's not changing or adding/replacing the old context menu with the new one

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 559 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 464 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans