Search This Blog

Wednesday, August 24, 2011

Filter Records in Form using Code

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 Custgroup 20.

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

QueryBuildRange CustGroupQBR;
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();

CustGroupQBR= this.query().dataSourceName('CustTable').addRange(fieldnum(CustTable,CustGroup));
}
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()
{ ;

CustGroupQBR.value(queryvalue('20'));

super();
}
You're done! When you open the form, your customer records are filtered, you only get the customers with CustGroup 20 set up.

No comments:

Post a Comment

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.