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...
Unanswered

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

(0) ShareShare
ReportReport
Posted on by

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)
  • Martin Dráb Profile Picture
    239,449 Most Valuable Professional on at

    By the way, it's in a wrong forum (as the title says, it belongs to an F&O 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

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 April Top 10 Community Leaders

These are the community rock stars!

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

#1
11manish Profile Picture

11manish 139

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 58 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans