Monday, August 13, 2007

Build your own form query, Dynamics AX 4.01 forms

lately I have been doing alot of modifications related to queries on forms, and I have also been assisting some colleages and in short I want to share the some situations I have been facing and how to deal with them.


Modify a standard query on a form, adding a ranges.


This excersize is pretty straight forward and has been done serveral places in std Dynamics AX. One common place to look for the implementation is custTrans where it is possible to switch view on the form to view either closed or open transactions.



Here there are declared a varable on the form method classdeclaration called criteriaOpen







Next the method init and executeQuery method on the datasourse Custtrans have been overwritten with the following relevant code



Init after call to super();



criteriaOpen = this.query().dataSourceNo(1).addRange(fieldnum(CustTrans,Closed));



one common error I have seen made related to this piece of code here is the use of addDataSource() instead of dataSourceNo(), or DataSourceTable(). What would actually happen is that the table would be added as a second Datasource producing a result that could be unpredictable.




ExecuteQuery before call to super();



criteriaOpen.value('value as string');




As mentioned before this is all pretty straight forward and many Dynamics AX developers have seen this design many times.




But what if the situation suddenly became a little bit more complicated and we actually needed to clear already autocreated ranges or we had to actually Add a table related to the datasource already in the query.




Well here is my suggestion




I would create a new query and use instead of the already existing query. That would make me in control over what happend in the Query and the order of the related tables. The code would look like this




Before super(); in the dataSource init




public void init() // of the datasource

{

Query query;

;

query = new Query();

tablename_ds.autoQuery(false); // we do not want to have the original query build

// and we build our own

queryBuildRange.addDatasource(tablenum('tablename');

queryBuildRange.value(sysquery::value('ány value'));

tablename_ds.query(query);// we use our own query



}




Happy querying
















No comments: