Thursday, 23 April 2015

How to filter records in a form by code in AX 2012

How to set range in a form by code in AX 2012

The standard filter functionality in Ax forms is a neat and powerful feature.
Using this filter functionality in your code is something you'll definitely use at some point in time as a programmer.

Although it's possible to do it in a single line of code, I prefer a 3 step solution. That way it's more flexible.
Let me show you by example. We'll filter the customers records in form CustTable, only showing customers with currency USD.

Step 1: Declare a class variable
In the ClassDeclaration method of the form, define a range.

QueryBuildRange CurrencyQBR;

Step 2: Instantiate the new range.
In the init method on the datasource of the form, you assign the range to a specific field (after the super call).

public void init()
{
super();

CurrencyQBR = this.query().dataSourceName('CustTable').addRange(fieldnum(CustTable,Currency));
}

Step 3: In the last step, you assign a value to the range.
This is done in the executeQuery method on the same datasource of the form. Before the super call. Like this:

public void executeQuery()
{ ;

CurrencyQBR.value(queryvalue('USD'));

super();
}

You're done! When you open the form, your customer records are filtered, you only get the customers with currencycode USD set up.

Like I said in the intro of this post, this can be done in one line of code as well.
In the init method of the form datasource, after the super call, place this code:

this.query().dataSourceName('CustTable').addRange(fieldnum(CustTable,Currency)).value(queryvalue('USD'));
But this way, it's fixed. If you choose the 3 step method, you could for example use a variable in the range value. The way to go would be to place an input field on your form, get the value from it and supply it in the executeQuery method.
For example like this:
public void executeQuery()
{ ;

CurrencyQBR.value(queryvalue(MyInputField.text()));

super();
}

Just make sure the executeQuery method is executed, thus applying the desired filter (maybe be using a button on your form to activate it).
Of course it's possible to combine multiple querybuildranges.

Example: 

Let me show you by another example: We'll filter the Sales PackingSlip records in form MSASNShipmentExport (new developed), only showing PackingSlip records on the basis of Financial Dimension "Region" as per mentioned screen shots.





Step-1:
declare the class variables in the ClassDeclaration method of the form, define datasource and range.

    //<declared> by Vimal ...
    DimensionAttributevaluesetitem  DimensionAttributevaluesetitemLoc;    
    QueryBuildDataSource                 qbds, qbdsdimensionValue;
    QueryBuildRange                         qbrIsExport, qbrdimValue;
    //<end> by Vimal

Step 2: Instantiate the new range.
In the init method on the datasource of the form, you assign the range to a specific field (after the super call).

public void init()
{
    Query       query; //<declared> by Vimal

    super();

    //<start> declared by Vimal on April 23, 2015
    // to initialise range for filtering records as per Financial dimension "Region"
    query = CustPackingSlipTrans_ds.query();
    qbds = query.dataSourceTable(tablenum(CustPackingSlipTrans));
    qbds.clearRanges();

    qbrIsExport = qbds.addRange(fieldnum(CustPackingSlipTrans,MSASNExport)); //.value(IsExport.valueStr());
    qbdsdimensionValue = qbds.addDataSource(tableNum(DimensionAttributevaluesetitem));
    qbdsdimensionValue.addLink(fieldNum(DimensionAttributevaluesetitem, DimensionAttributeValueSet), fieldNum(CustPackingSlipTrans, DefaultDimension));
    qbdsdimensionValue.joinMode(JoinMode::ExistsJoin);
    qbrdimValue = qbdsdimensionValue.addRange(fieldNum(DimensionAttributevaluesetitem, DisplayValue));
    //<end> by Vimal
}

Step 3: In the last step, you assign a value to the range.
This is done in the executeQuery method on the same datasource of the form. Before the super call. Like this:

//<created> by Vimal .....
public void executeQuery()
{   
        
    
    qbrdimValue.value(FilterBy.valueStr()); //here, FilterBy is the StringEdit control name
    qbrIsExport.value(IsExport.valueStr()); //here, IsExport is the StringEdit control name
    //<end> by Vimal
    
    super();
.......................
}

You're done! When you open the form, your packingslip records are filtered, you only get the records with Financial Dimension "Region" set up.
Like this:


Happy DAXing

No comments:

Post a Comment