Skip to main content

Notifications

Announcements

No record found.

Supply chain | Supply Chain Management, Commerce
Unanswered

Hiding menu item using COC

Posted on by 1,789
Hi Experts,
 
I am trying to hide(visible=false) menu item "buttonUpdatePackingSlip" for specific user. I created a table and assign userid there and checking userid if exists hide the menu button.
But getting error using below code.
 
[ExtensionOf(formStr(SalesTable))]
public final class SFA_SalesTable_Packingslip_Extension
{

    public void init()


    {
        next    init();

        if (this.isUserInConfig())
        {
            // Find the "Generate Packing Slip" button and make it invisible
            FormButtonControl packingSlipButton = this.design().controlName('buttonUpdatePackingSlip');
            if (packingSlipButton)
            {
                packingSlipButton.visible(false);
               
            }
        }

    }

    private boolean isUserInConfig()
    {
        SFA_PackingSlip_UserId userConfig;
        select firstOnly RecId from userConfig
            where userConfig.UserId == curUserId();
        return userConfig.RecId != 0;
    }

    

}
 
Thanks,
Faiz
  • Martin Dráb Profile Picture
    Martin Dráb 229,426 Most Valuable Professional on at
    Hiding menu item using COC
    Auto Declaration is needed when you refer to a control from the given form, but it's even more relaxed in extensions. There you can refer to all controls by name, regardless of the value of Auto Declaration.
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 290,946 Super User 2024 Season 2 on at
    Hiding menu item using COC
    Hi Faiz,
     
    The control buttonUpdatePackingSlip has the property Auto declaration enabled. You can try to directly call the button by its name in the COC instead of using another variable as Martin already mentioned in his first reply. 
  • Martin Dráb Profile Picture
    Martin Dráb 229,426 Most Valuable Professional on at
    Hiding menu item using COC
    The error message means that the form threw an exception, therefore you seem to have a bug in your code. Use the debugger to find out what failed.
     
    You can use security to deny access to the button in SalesTable form while keeping keeping access to the menu item button as such (used at different places).
     
    I see no benefit in switching from CoC to an event handler; it just makes code more complicated. And calling your code again for every record (by using Activated event) isn't needed, because the value doesn't depends on the selected sales order.
  • faiz7049 Profile Picture
    faiz7049 1,789 on at
    Hiding menu item using COC
    Hi Martin,
     
    Error "The menu item with name salestablelistpage could not be opened".
     
    Security role does not work for our scenario because we are using advanced warehouse module means user is posting packing slip from other form not from SalesTable and SalesTableListPage. We want to invisible this menu item on  SalesTable and SalesTableListPage.
     
    Security privilege "Deny" will stop them from everywhere.
     
    Init method or Activaced event handler. Which one is correct below is event handler.
     
    [FormEventHandler(formStr(SalesTableListPage), FormEventType::Activated)]
        public static void SalesTableListPage_OnActivated(xFormRun sender, FormEventArgs e)
        {
            FormRun element = sender as FormRun;
            if (element.isUserInConfig())
            {
                // Find the "Generate Packing Slip" button and make it invisible
                FormButtonControl packingSlipButton = element.design().controlName(formControlStr(SalesTableListPage,buttonUpdatePackingSlip));
                if (packingSlipButton)
                {
                    packingSlipButton.visible(false);
                   
                }
            }
    
           
    
        }
    
            private boolean isUserInConfig()
            {
            SFA_PackingSlip_UserId userConfig;
            select firstOnly RecId from userConfig
            where userConfig.UserId == curUserId();
            return userConfig.RecId != 0;
            }
     
  • Martin Dráb Profile Picture
    Martin Dráb 229,426 Most Valuable Professional on at
    Hiding menu item using COC
    Do you think that we can resolve your error if you don't tell us what error you got?
     
    Why don't you simply use security to control access to the button?
     
    If you really must do it in code, I strongly recommend extending the standard logic instead of creating something similar at a different place. Whether buttonUpdatePackingSlip should be enabled is controlled by SalesTableInteractionHelper.parmCanPackingslipBeUpdated(), which calls SalesTableType.canPackingslipBeUpdated().
     
    By the way, if making the button invisible by code would be right approach, you could replace
    FormButtonControl packingSlipButton = this.design().controlName('buttonUpdatePackingSlip');
    if (packingSlipButton)
    {
        packingSlipButton.visible(false);
    }
    with mere buttonUpdatePackingSlip.visible(false).
     
    And one more thing - never hard-code application elements as strings. If using controlName() was appropriate, you should use controlName(formControlStr(SalesTable, buttonUpdatePackingSlip) instead of controlName('buttonUpdatePackingSlip'). Then correctness of the reference will be verified by the compiler and you'll get cross references.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,426 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans