Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.
Monday, 4 May 2015
Querying date effective tables in Microsoft Dynamics AX 2012 R3
In Microsoft Dynamics AX 2012, valid time state tables (date effective tables) were introduced to help organizations keep track of data that changes over time. For example, currency exchange rates change over time and it is necessary to view exchange rate data at a specific point in time.
For this blog, the exchange rate table will be used as an example.
This blog discusses how developers can query data in date effective tables and examines the results when using forms, queries, views and X++ code. If not properly understood by the developer, incorrect or incomplete data will be returned when querying the database.
Exchange rate table
The exchange rate table holds exchange rate data between two (2) currencies. The standard Microsoft Dynamics AX form can show current, past and future the exchange rate data as show below.
Creating custom forms
A custom screen was created to display the exchange rate table. The exchange rate table was used as a data source and a grid was added to display the data.
The data that is returned only shows data that is current based on a current date of 1/2/2015. Prior and future exchange rates are excluded.
The ExchangeRate datasource property ‘ValidTimeStateAutoQuery’ is used to control what data is returned.
Change this to ‘DateRange’ and all records will be returned.
Querying the data using a query
A custom query was created to query the ExchangeRate table.
The following code is used to run this query:
static void AXMExchangeRateQuery(Args _args)
query = new query(queryStr(AXMExchangeRateQuery));
queryRun = new QueryRun(query);
ExchangeRate = queryRun.get(tableNum(ExchangeRate));
info(strFmt("%1 %1", ExchangeRate.ValidFrom, ExchangeRate));
Running the query returns only current records.
It is possible to change the behavior of the query results set to return data at a previous point in time, for example prior exchange rates.
The following code can be used to query data for different date ranges:
Use the validateTimeStateASOfDate method to return data that is valid at a specific date. For example, if you want to know what the exchange rate should be for a transaction with a date of 3/7/2014, run the following code:
The following data is returned, again only current data is returned with a current date of 1/2/2015. It is important to note this as not all records are returned from the databse, despite no filtering applied in the X++ while select statement.
It is possible to return data as of a specific point in time using the validTimeState keyword.
The following code can be used to find data that is valid for a given data range:
Microsoft Dynamics AX Views can be created in the AOT to return data from data effective tables. This can be used to overcome issues when you need to return all data from a date effective table and cannot use the query object or X++ parameters to control the date effective options.
The following view was created in the AOT.
Using a view in a form data source
A new form was created and the view above was used as datasource.
The following records are shown, the date effective filters do not apply.
Using a view in a query
A custom query was created to query the ExchangeRate table using a view.
Running the code returns all records, the date effective filters do not apply.
Modifying the view properties
By default, using a view to display data form a date effective table disables the date effective search results, which is useful when it is necessary to always return all data. However, it is possible to enable the date effective parameters, by changing the view property below