web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Disable “+ New” button in LedgerChartOfAccounts form based on security role in D365FO

(0) ShareShare
ReportReport
Posted on by 50
 

Hi Experts,

I am working on role-based button enable/disable in the LedgerChartOfAccounts form in D365FO.

Requirement:

  • If the user is NOT assigned to a specific security role, the + New button should be disabled immediately when opening the LedgerChartOfAccounts form.
  • Currently, if I click + New, it navigates to the MainAccount form and when I come back, the button becomes disabled.

  • But I need the button to be disabled as soon as the LedgerChartOfAccounts form opens itself, without first clicking the button.

There is a overridden clicked() method for the button:

void clicked()
{
    Args args;
    FormRun mainAccountForm;

    args = new Args();
    args.name(formStr(MainAccount));
    args.caller(element);
    args.record(ledgerChartOfAccounts);

    args.openMode(OpenMode::New);

    mainAccountForm = classfactory.formRunClass(args);

    if (mainAccountForm)
    {
        mainAccountForm.init();
        mainAccountForm.run();
        mainAccountForm.wait();
        mainAccount_ds.research(true);
    }
}

I tried:

  • button.enabled(false)

  • allowCreate(false)

  • Form Activated event

  • Datasource Activated event

This is my Code:

 [FormEventHandler(formStr(LedgerChartOfAccounts), FormEventType::Activated)]
 public static void LedgerChartOfAccounts_OnActivated(xFormRun sender, FormEventArgs e)
 {
  
    
     FormButtonControl buttonNew;
     FormDropDialogButtonControl newFromTemplate;
   
     SecurityRole     securityRole;
     SecurityUserRole securityUserRole;
 
  
     buttonNew        = sender.design().controlName("ButtonNew");
     newFromTemplate  = sender.design().controlName("NewFromTemplate");
 
     FormDataSource chartOfAccount_ds = sender.dataSource(formDataSourceStr(LedgerChartOfAccounts, LedgerChartOfAccounts));
 
     Select firstonly securityRole
         where //securityRole.Name == "Admin" ||
           securityRole.Name == "Role to enable new/edit./del button in MA"
         join securityUserRole
         where securityUserRole.SecurityRole == securityRole.RecId
         && securityUserRole.User == curUserId();
     if (securityRole)
     {
         
    
         buttonNew.enabled(true);
         newFromTemplate.enabled(true);
     }
     else
     {
   
         buttonNew.enabled(false);
         newFromTemplate.enabled(false);
     }
 }
And i tried this init() also, but this also is not working.
[ExtensionOf(formStr(LedgerChartOfAccounts))]
final class TPZ_ChartOfAccount_Extension
{
    public void init()
    {
        next init();
        this.setCreatePermissionBasedOnRole();
    }
    private void setCreatePermissionBasedOnRole()
    {
        const str RoleName = 'Role to enable new/edit./del button in MA';
        FormRun formRun = this as FormRun;
        FormDataSource ledgerJournalTableDS = formRun.dataSource(formDataSourceStr(LedgerChartOfAccounts, LedgerChartOfAccounts));
        if (this.userHasSecurityRole(RoleName))
        {
            ledgerJournalTableDS.allowCreate(true);
        }
        else
        {
            ledgerJournalTableDS.allowCreate(false);
        }
    }
    private boolean userHasSecurityRole(str _roleName)
    {
        SecurityRole        securityRole;
        SecurityUserRole    securityUserRole;
        Select firstonly securityRole
                where //securityRole.Name == "System administrator" ||
                  securityRole.Name == _roleName
                join securityUserRole
                where securityUserRole.SecurityRole == securityRole.RecId
                && securityUserRole.User == curUserId();
        return securityRole.RecId != 0;
    }
}
 
 

But still the button is enabled when the form initially opens. Only after navigating to MainAccount and returning back does the button become disabled.

Has anyone faced this scenario? What is the recommended approach to disable the + New button immediately on form load based on security role?

Thank you.

 
Categories:
I have the same question (0)
  • Suggested answer
    Nishit.Parikh Profile Picture
    60 on at
     
    • Go to System Administration > Security > Security Configuration
     
    • In Privileges, search for: Maintain Chart of Accounts
      • Locate the display menu item: LedgerChartOfAccounts
      • Update the permission and set Create access to "Deny"
     
    • Publish the changes
      • Once published, the New button will be removed.
    Recommendation
    • Instead of modifying the existing privilege, it is recommended to:
      • Create a duplicate of the privilege
      • Apply the required changes to the copied version (e.g. set Create permission to Deny)
      • Assign the updated privilege to the relevant roles to restrict access
    • This approach helps to:
      • Avoid impacting existing configurations
      • Maintain a backup of the original privilege
      • Reduce risk to other roles or processes
     
     
     
     
     
  • Subramani Sivan Profile Picture
    359 on at
    we have to use the Init method for to enable and disable the new button and I haven't seen any issues in your code. Did you debugged your code in VS and checked whether it's working as you expected.
     
    Thanks,
    Subra

    If this helped, please mark it as "Verified" for others facing the same issue
  • Adis Profile Picture
    6,657 Super User 2026 Season 1 on at
    Hey,
     
    I fully agree with @Nishit.Parikh to work with the security diagnostics instead of coding. You need a release for any changes, whereas the security configuration offers instant changes.
     
    You can use the Deny option for the button, its valid and will work. I am just thinking about the following and this is my personal experience, I am not saying its the best approach for everyone.
     
    • I am not a big fan of the DENY option. Let some time pass, months a year or someone else is in charge for the security. It is not enough to assign someone the appropriate role for the NEW button. You have to remove the role that has the Deny option for the user and then the question is what else are you removing that this user still needs. And you have to know which role has the duty and privilege that denies the NEW button.
    • So, first of all, I would generally not modify standard roles, duties and privileges.
    • You can copy the privilege and change the display menu item from Grant to Unset. Then you create a new privilege that has the display menu item with Grant. Assign the privilege to an existing or new duty\role, which are then assigned to the users that are allowed to use the NEW button.
     

    Kind regards, Adis

     

    If this helped, please mark it as "Verified" for others facing the same issue

    Keep in mind that it is possible to mark more than one answer as verified

     
     
  • Roja C-30061119-0 Profile Picture
    50 on at

    Hi Subramani Sivan,

    Thank you for your response.

    I checked with the debugger, and the code is triggering correctly. However, the changes are not reflecting on the form while opening.

    I also tried using the OnActivated event handler. In that case, the button gets disabled only after clicking or activating the form, but when I reopen the form, the button is enabled again by default.

    My requirement is to disablethe button immediately while opening the form itself.

    Could you please suggest which event or approach would be best for this scenario?
     

    Thank you.

  • Suggested answer
    Subramani Sivan Profile Picture
    359 on at
    Hi Roja,
     
    Could you use on intialilizing event and let me know whther you have any issue. 
     
    Thanks,
    Subra

    If this helped, please mark it as "Verified" for others facing the same issue
  • Roja C-30061119-0 Profile Picture
    50 on at

    Hi Subramani Sivan,

    Thank you for your suggestion.

    I tried using the intialilizing event as well, but it is still not working for the New/Edit/Delete buttons while opening the form.

    Currently, I am using the OnActivated event, and the role validation logic is working correctly. The debugger also hits properly, and other controls are getting updated based on the role assignment.

    However, these two buttons are not getting disabled when the user role is not assigned. They remain enabled while opening the form and only reflect after activating/clicking the form again.

    In this image, the system-generated button is becoming disabled correctly. However, the below New and New template buttons are still enabled.

    I want to disable these buttons as well when the user role is not assigned.

    Current Code:

    internal final class TPZ_MainAccount_EventHandler
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [FormEventHandler(formStr(MainAccount), FormEventType::Activated)]
        public static void MainAccount_OnActivated(xFormRun sender, FormEventArgs e)
        {
            FormCommandButtonControl editButton, deleteButton, newButton;
            #SysSystemDefinedButtons
            UserInfo         userInfo;
            SecurityRole     securityRole;
            SecurityUserRole securityUserRole;
            ;
            newButton= sender.control(sender.controlId(#SystemDefinedNewButton)) as FormCommandButtonControl;
            editButton= sender.control(sender.controlId(#SystemDefinedViewEditButton)) as FormCommandButtonControl;
            deleteButton= sender.control(sender.controlId(#SystemDefinedDeleteButton)) as FormCommandButtonControl;
            FormDataSource mainAccount_ds = sender.dataSource(formDataSourceStr(MainAccount, MainAccount));
            Select firstonly securityRole
                    where //securityRole.Name == "System administrator" ||
                      securityRole.Name == "Role to enable new/edit./del button in MA"
                    join securityUserRole
                    where securityUserRole.SecurityRole == securityRole.RecId
                    && securityUserRole.User == curUserId();
            if (securityRole)
            {
                newButton.enabled(true);
                editButton.enabled(true);
                deleteButton.enabled(true);
                //mainAccount_ds.allowEdit(true);
            }
            else
            {
                newButton.enabled(false);
                editButton.enabled(false);
                deleteButton.enabled(false);
                //mainAccount_ds.allowEdit(false);
            }
        }
        [FormEventHandler(formStr(LedgerChartOfAccounts), FormEventType::Activated)]
        public static void LedgerChartOfAccounts_OnActivated(xFormRun sender, FormEventArgs e)
        {
            FormCommandButtonControl editButton, deleteButton, newButton;
            FormControl  editMainAccount, deleteMainAccount;
            FormButtonControl buttonNew;
            FormDropDialogButtonControl newFromTemplate;
            #SysSystemDefinedButtons
            SecurityRole     securityRole;
            SecurityUserRole securityUserRole;
            ;
            newButton    = sender.control(sender.controlId(#SystemDefinedNewButton)) as FormCommandButtonControl;
            editButton   = sender.control(sender.controlId(#SystemDefinedViewEditButton)) as FormCommandButtonControl;
            deleteButton = sender.control(sender.controlId(#SystemDefinedDeleteButton)) as FormCommandButtonControl;
            //buttonNew        = sender.design().controlName("ButtonNew");
            buttonNew        = sender.control(sender.controlId("ButtonNew")) as FormButtonControl;
            editMainAccount  = sender.design().controlName("EditMainAccount");
            deleteMainAccount= sender.design().controlName("DeleteMainAccount");
            newFromTemplate  = sender.design().controlName("NewFromTemplate");
            FormDataSource chartOfAccount_ds = sender.dataSource(formDataSourceStr(LedgerChartOfAccounts, LedgerChartOfAccounts));
            Select firstonly securityRole
                where //securityRole.Name == "System administrator" ||
                  securityRole.Name == "Role to enable new/edit./del button in MA"
                join securityUserRole
                where securityUserRole.SecurityRole == securityRole.RecId
                && securityUserRole.User == curUserId();
            if (securityRole)
            {
                newButton.enabled(true);
                editButton.enabled(true);
                deleteButton.enabled(true);
                buttonNew.enabled(true);
                editMainAccount.enabled(true);
                deleteMainAccount.enabled(true);
                newFromTemplate.enabled(true);
            }
            else
            {
                newButton.enabled(false);
                editButton.enabled(false);
                deleteButton.enabled(false);
                buttonNew.enabled(false);
                editMainAccount.enabled(false);
                deleteMainAccount.enabled(false);
                newFromTemplate.enabled(false);
            }
        }
    }

    Could you please suggest if these buttons are controlled differently in the standard form or if there is any additional refresh method required?

    Thank you.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 134

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 79 Super User 2026 Season 1

#3
CP04-islander Profile Picture

CP04-islander 57

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans