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 :
Supply chain | Supply Chain Management, Commerce
Answered

Hiding menu item using COC

(2) ShareShare
ReportReport
Posted on by 2,382
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
I have the same question (0)
  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    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.
  • faiz7049 Profile Picture
    2,382 on at
    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
    237,990 Most Valuable Professional on at
    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.
  • André Arnaud de Calavon Profile Picture
    301,210 Super User 2025 Season 2 on at
    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
    237,990 Most Valuable Professional on at
    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.
  • faiz7049 Profile Picture
    2,382 on at
    Hi Martin & Andre,
     
    Customer requirement now is, they want to check item group not current user id. buttonUpdatePackingSlip always invisible only visible if item group  exists in SFA_PaclingSlip_ItemGroup. How to check item group in method isItemGroupInConfig. buttonUpatePackingSlip in 2 forms called SalesTable and SalesTableListPage.
     
        [FormEventHandler(formStr(SalesTableListPage), FormEventType::Activated)]
        public static void SalesTableListPage_OnActivated(xFormRun sender, FormEventArgs e)
        {
           FormRun element = sender as FormRun;
            if (element.isItemGroupInConfig())
            {
                // Find the "Generate Packing Slip" button and make it invisible
                FormButtonControl packingSlipButton = element.design().controlName(formControlStr(SalesTableListPage,buttonUpdatePackingSlip));
               if (packingSlipButton)
               {
                    packingSlipButton.visible(true);
                   
                }
    		}       
    
        }
    
           private boolean isItemGroupInConfig()
            {
           SFA_PackingSlip_ItemGroup itemGroup;
          select firstOnly RecId from itemGroup
         
          return itemGroup.RecId != 0;
           }
    
     
  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    All I know about your SFA_PackingSlip_ItemGroup table is what you told me, which is nothing. But always checking the first record in the table doesn't make a good sense to me. You need to think about which item group you want to check; I guess it may be something associated with the item on a sales line or to a packing slip. Then you'll create CoC extension of active() method of the right data source and find the data related to the currently selected record.
  • faiz7049 Profile Picture
    2,382 on at
    Hi Martin,
     
    How to call isItemGroup(itemGroupId) method and where to add this code in eventhandler.
    FormButtonControl packingSlipButton = element.design().controlName(formControlStr(SalesTable,buttonUpdatePackingSlip));
    packingSlipButton.visible(true);
     
     
     
    internal final class SFA_SalesTable_ItemId_PackingSlip_EventHandler
    {
    
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
        public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
        {
            
            SalesLine    salesLine;
            ItemId         itemId;
            ItemGroupId     itemGroupId;
            InventTable     inventTable;
            salesLine = sender.cursor();
            if(salesLine)
    
            {
                itemId=salesLine.ItemId;
                itemGroupId = inventTable::find(itemId).itemGroupId();
            }
    
         
        }
        
        boolean isItemGroup(ItemGroupId _itemGroupId)
        {
            SFA_PackingSlip_ItemGroup packingItemGroup;
            select RecId    from packingItemGroup
                    where packingItemGroup.ItemGroupId==_itemGroupId;
    
            return packingItemGroup.RecId !=0 ;
        }
    }
    
     
  • faiz7049 Profile Picture
    2,382 on at
    Hi Experts,
     
    Could you please tell me check what's wrong in my approach.
     
    internal final class SFA_SalesTable_ItemId_PackingSlip_EventHandler
    {
    
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
        public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
        {
            
            SalesLine    salesLine;
            ItemId         itemId;
            ItemGroupId     itemGroupId;
            InventTable     inventTable;
            FormRun formRun = sender.formRun(); 
            salesLine = sender.cursor();
    
            if(salesLine)
    
            {
                itemId=salesLine.ItemId;
                inventTable = inventTable::find(itemId);
    
                if (inventTable)
                {
                    itemGroupId = inventTable.itemGroupId();
                    if (!SFA_SalesTable_ItemId_PackingSlip_EventHandler::isItemGroup(itemGroupId))
                    {
                        FormButtonControl packingSlipButton = formRun.design().controlName(formControlStr(SalesTable, buttonUpdatePackingSlip)) as FormButtonControl;
                        if (packingSlipButton)
                        {
                            packingSlipButton.visible(false);
    
                        }
                    }
                }
            }
        }
        
       private static boolean isItemGroup(ItemGroupId _itemGroupId)
        {
            ItemGroupId localItemGroupId;
            localItemGroupId=_itemGroupId;
            SFA_PackingSlip_ItemGroup packingItemGroup;
            select RecId    from packingItemGroup
                    where packingItemGroup.ItemGroupId==localItemGroupId;
    
            return packingItemGroup.RecId !=0 ;
        }
    }
     
  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at
    Please tell us what problem you have with your code.
     
    What I say for sure is that your code is over-complicated. This will be easier to work with:
    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
    public static void salesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {
        SalesLine salesLine = sender.cursor();
        InventTable inventTable = salesLine.inventTable();
    
        if (inventTable)
        {
            ItemGroupId itemGroupId = inventTable.itemGroupId();
            if (!SFA_SalesTable_ItemId_PackingSlip_EventHandler::isItemGroup(itemGroupId))
            {
                FormRun formRun = sender.formRun(); 
                FormButtonControl packingSlipButton = formRun.design().controlName(formControlStr(SalesTable, buttonUpdatePackingSlip)) as FormButtonControl;
                packingSlipButton.visible(false);
            }
        }
    }
        
    private static boolean isItemGroup(ItemGroupId _itemGroupId)
    {
        SFA_PackingSlip_ItemGroup packingItemGroup;
        
        select firstOnly RecId from packingItemGroup
            where packingItemGroup.ItemGroupId == _itemGroupId;
    
        return packingItemGroup.RecId != 0;
    }

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 > Supply chain | Supply Chain Management, Commerce

#1
Laurens vd Tang Profile Picture

Laurens vd Tang 305 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 67 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans