Friday, 29 September 2017
Setup AOT Query range with CreatedBy as curren
SysQueryRangeUtil::currentUserId Method [AX 2012]
e.g. Using Methods in Table Filters & Query Ranges in Dynamics AX 2012
In Dynamics AX, there is a class called SysQueryRangeUtil that can be utilized in both query ranges and table filters. Using methods from this class allow you to be very precise about what dates you want to use in reports or for filtering your data.
Let’s say you have a report that you always want to run to see orders with shipping dates of the next day. It is possible to do so by using one of the methods from the SysQueryRangeUtil. The use of the letter ‘t’ will work for today’s date, but when you try to add days to it, it doesn’t work in reports. Instead, I will use the currentdate() method and add 1 to it.
Note: All methods & expressions must be surrounded by parentheses as shown below.
Note: All methods & expressions must be surrounded by parentheses as shown below.
Figure 1 – Filtering the requested ship dates in an AX query for tomorrow (current day + 1)
On any form with a grid, you filter your data by pressing Ctrl+G. If I were to want to see open customer invoices from the last 90 days, I would filter my open customer invoices form and use the method (dayRange(-90,0)). The first number represents how many months backward from this month, and the second represents how many months forward. The same sorts of things can be done for the monthRange(), yearRange(), and dateRange() methods. The best part about this is that you can of course save these filters to create views that you might use on a daily basis.
Figure 2 – Searching the grid for the past 90 days
If you are creating your query ranges in code, these methods can also be utilized whenever you are setting them. Definitely be sure to check out the SysQueryRangeUtil class as there are many more methods to use.
Here are some of them:
currentCustomerAccount()
currentVendorAccount()
currentUserId()
currentDate()
dateRange()
day()
dayRange()
greaterThanDate()
greaterThanUtcDate()
greaterThanUtcNow()
lessthanDate()
lessthanUtcDate()
lessthanUtcNow()
monthRange()
yearRange()
currentCustomerAccount()
currentVendorAccount()
currentUserId()
currentDate()
dateRange()
day()
dayRange()
greaterThanDate()
greaterThanUtcDate()
greaterThanUtcNow()
lessthanDate()
lessthanUtcDate()
lessthanUtcNow()
monthRange()
yearRange()
e.g. Custom Query Range Functions using SysQueryRangeUtil
You've probably seen these requests before. Users want to submit some report or other functionality to batch, and the query should always be run for "yesterday". It's a typical example where, as a user, it would be handy to be able to use functions in your query range. Well, you can. And in fact, you can make your own, very easily!
Enter the class "SysQueryRangeUtil". All it contains is a bunch of static public methods that return query range values. For example, there is a method called "day" which accepts an optional integer called "relative days". So, in our example of needing a range value of "tomorrow", regardless of when the query is executed, you could use day(-1) as a function. How this works in a range? Just open the advanced query window and enter your function call within parentheses.
Let's make our own method as an example. Add a new method to the SysQueryRangeUtil class, and enter the following, most interesting code you've ever encountered.
public static str customerTest(int _choice = 1)
{
AccountNum accountNum;
switch(_choice)
{
case 1:
accountNum = '1101';
break;
case 2:
accountNum = '1102';
break;
}
return accountNum;
}
So, this accepts an options parameter for choice. If choice is one (or choice is not specified), the function returns 1101, if 2 it returns 1102. Save this method and open a table browser window on the CustTable table. Type CTRL+G to open the grid filters. In the filter field for the AccountNum field, enter: (customerTest(1)).
So, the string returned from the method is directly put in the range. So, you could do all sort of interesting things with this, of course. Check out some of the methods in the SysQueryRangeUtil as examples.
Happy DAXing...
Subscribe to:
Posts (Atom)