How to toggle button states using the FormButtonManager class in Microsoft Dynamics AX 2009?

Generally in Dynamics AX, we write seperate methods for controlling the state of buttons on forms and call these from the data source's active method. In Dynamics AX 2009, a new framework was introduced for this need. This is the FormButtonManager class.

This class provides all the necessary methods to tap into this framework and control the state of the buttons. The idea is similiar to what we are used to do but this class provides a more clean approach.

Let us take a practical approach to using this framework.

We will work on the Customers form (CustTable).

On the Customers form, we have a menu button named "Project Control". Now the requirement is that this button should be enabled only for customer's who are associated with projects.

Now we need a provider class to provide us with the various rules which control the button states. For this purpose we will have to create a class. Each rule is created as a method in the class which returns a boolean value representing the state of the button. This class is known as the Rule Implementer class.

If a class can be used as as a rule implementer class, it needs to implement the ButtonStateProvider interface.

Let us create a new class and implement the ButtonStateProvider interface.

We will need to implement three methods in the newly created class.

  1. clearCache - Handles clearing of client cache values. In our case, we will not be storing any values to the client cache so this can be left empty

  2. determineButtonStates - Set buttons states for a particular rule. We will not be needing this either so this can be left empty.

  3. setDatasources - Initialize the datasources. The datasource will provide the record based on which we will evaluate our rule. Upto 5 datasources can be specified. Only the first one is mandatory


Apart from these, we will also need to create a new method to implement our rule.

The code for the CustTableRules class ,the three implemented methods and our rule method is given below.

public class CustTableRules implements ButtonStateProvider
{
CustTable custTable;
}

public void clearCache()
{
;
}

public void determineButtonStates(str _ruleFunctionName)
{
;
}

public void setDatasources(Common datasources1, Common datasources2 = null, Common datasources3 = null, Common datasources4 = null, Common datasources5 = null)
{
;
custTable = datasources1;
}

///
/// This method checks if the current customer is associated to any project.
///

///
/// True if the customer is associated with a project.
///

public boolean checkCustomerHasProject()
{
ProjTable projTable;
ProjInvoiceTable projInvoiceTable;
boolean ret;
;

select firstonly RecId from projTable
where projTable.CustAccount == custTable.AccountNum;

if (projTable)
{
ret = true;
}
else
{
select firstonly RecId from projInvoiceTable
where projInvoiceTable.InvoiceAccount == custTable.AccountNum;

if (projInvoiceTable)
{
ret = true;
}
}

return ret;
}

Now declare a FormButtonManager object in CustTable form. In the init() method, let us instantiate this and set some methods.


formButtonManager = FormButtonManager::construct();
formButtonManager.parmRuleImplementer(custTableRules);

And after the call to super() in the init method, add the following lines.


formButtonManager.setDatasources(custTable);
formButtonManager.registerButtonRule(ButtonGroup, CtrlProjectControl, methodstr(CustTableRules, checkCustomerHasProject));

The registerButtonRule() method takes four parameters, the last one is optional.

- The first parameter is the button group in which we want to control a button.
- The second parameter is the actual button we want to control.
- The third parameter is the method name which is acting as a rule here.
- The last parameter is the RuleEffectType value. There can be two effects, enable/disable the button or hide/unhide the button. The default behavior is to enable/disable the button. If you want to hide/unhide the button, pass the last argument as ButtonRuleEffectType::Visible

Now, the final step is to call the formButtonManager.refresh() method in the custTable datasource's active method.

Add this line in the active() method.


formButtonManager.refresh();

And we are done. On my Contoso dataset, there are no projects associated with customer 1101 so the Project Control button is disabled but when I select customer 2003, the button is enabled.

You can download the XPO for this project from here.

Please feel free to comment or question if you did not follow anything here.

That is all for today, do check back again later.

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

How to refer to form controls in code without setting the AutoDeclaration property in Dynamics AX?