Thursday, June 20, 2013

Accessing the correct QueryBuildDataSource for a Form DataSource

Traditionally within Forms it has been rather cumbersome to get to the QueryBuildDataSource for a specific FormDataSource within Forms. You had to write code such as this:

public void FormDataSource::init()
{
    QueryBuildDataSource qbds;

    super();

    qbds = this.query().dataSourceTable(tableNum(CustTable));
}

That’s all fine and dandy if you have a simple Form, but on some Forms you might have multiple instances of CustTable, especially with the advent of ReferenceDataSources. In these cases you might fall back to using the name of the FormDataSource to ensure you get the correct QueryBuildDataSource:

public void FormDataSource::init()
{
    QueryBuildDataSource qbds;

    super();

    qbds = this.query().dataSourceName(this.name());

}

This is also problematic in the cases that a different query is applied to the Form, such as in the CopyCallerQuery from a List Page to a Details Page. This makes it all very confusing and difficult, and to tell you the truth, we have a very long method in the kernel that finds the correct QueryBuildDataSource for the respective FormDataSource. The good news is those methods are now exposed to X++

With Dynamics AX 2012 you can now use the methods FormDataSource::queryBuildDataSource() and FormDataSource::queryRunQueryBuildDataSource(). The first method will return you the QueryBuildDataSource in the FormDataSource.query(), while the second will return you the QueryBuildDataSource in the FormDataSource.queryRun().query(). There'll be another blog entry discussing when using each is appropriate, but the short of it is use the first whenever you want to modify the QueryBuildDataSource and have the modification stay throughout the life of the Form (such as adding developer ranges that you never want the user to be able to change). The second should be used when you want to make modifications that will just live for the current executeQuery() call and will be cleared whenever the user clicks on the "Clear Filters" button.
 

No comments: