How to get list page's datasource in Dynamics AX 2012?

Recently, I had a requirement where I needed to disable a button on a list page if the records selected did not meet a particular condition. Let me explain you this with an example. Say I have a new button on the Projects list page. This button should be enabled only if all projects selected have the same project contract ID. If the contract ID is different for the selected projects, the button should be disabled.
So how can we achieve this? On a form, this can be done in a very straight forward way. Just loop through all the selected records and check if the project contract IDs are different. If they are, just disable the button. But on a list page, the form datasource is not readily available. After some investigation, I found out a way of doing this.

As you must be knowing, all list pages in Dynamics AX 2012 are controlled via interaction classes. This is the new framework model. If you are new to this and dont know about these, I'll recommend you learn more on this on the How to: Create a List Page Form page on MSDN.

Coming back to the post, the interaction class has a method selectionChanged() which is fired every time a record is selected or deselected on the list page. We can use this method to check the project contract ID of the selected projects. So, our logic should go into this method.

Now how to retrieve the form's datasource? To do that, I first got hold of the active record on the list page and then extracted the datasource object associated with it. The code to do that is

this.listPage().activeRecord(queryDataSourceStr(ProjTable_NoFilter, ProjTable)).dataSource();

Notice how I have used the intrinsic function queryDataSourceStr() here to get the datasource. You can read more about this function in my previous post on queryDataSourceStr.

I've created a new helper method called enableNewButton to return me a boolean value to enable/disable the button.

public boolean enableNewButton(FormDataSource  _formDataSource)
{
    ProjTable   projTableLocal;

    boolean     ret = true;

    for (projTableLocal = _formDataSource.getFirst(1); projTableLocal; projTableLocal = _formDataSource.getNext())
    {
        if (selectedProjects.elements() && !selectedProjects.in(projTableLocal.ProjInvoiceProjId))
        {
            ret = false;
            break;
        }

        selectedProjects.add(projTableLocal.ProjInvoiceProjId);
    }

    return ret;
}

I call this method from the selectionChanged() method.

public void selectionChanged()
{
    super();

    this.enableButton(newButtonName,
    this.enableNewButton(this.listPage().activeRecord(queryDataSourceStr(ProjTable_NoFilter, ProjTable)).dataSource()));
}

That is it. You can download the objects I've used in this sample from here.

Thats all for today, do check back soon for more.

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

The field with ID '0' does not exist in table - Cause and resolution.