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
+ Newbutton should be disabled immediately when opening theLedgerChartOfAccountsform. -
Currently, if I click
+ New, it navigates to theMainAccountform and when I come back, the button becomes disabled. -
But I need the button to be disabled as soon as the
LedgerChartOfAccountsform 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:
public static void LedgerChartOfAccounts_OnActivated(xFormRun sender, FormEventArgs e)
{
FormButtonControl buttonNew;
FormDropDialogButtonControl newFromTemplate;
SecurityRole securityRole;
SecurityUserRole securityUserRole;
newFromTemplate = sender.design().controlName("NewFromTemplate");
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();
{
buttonNew.enabled(true);
newFromTemplate.enabled(true);
}
else
{
newFromTemplate.enabled(false);
}
}
And i tried this init() also, but this also is not working.
final class TPZ_ChartOfAccount_Extension
{
public void init()
{
next init();
}
{
const str RoleName = 'Role to enable new/edit./del button in MA';
FormDataSource ledgerJournalTableDS = formRun.dataSource(formDataSourceStr(LedgerChartOfAccounts, LedgerChartOfAccounts));
{
ledgerJournalTableDS.allowCreate(true);
}
else
{
ledgerJournalTableDS.allowCreate(false);
}
}
{
SecurityRole securityRole;
SecurityUserRole securityUserRole;
where //securityRole.Name == "System administrator" ||
securityRole.Name == _roleName
join securityUserRole
where securityUserRole.SecurityRole == securityRole.RecId
&& securityUserRole.User == curUserId();
}
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.

Report
All responses (
Answers (