Tuesday 25 February 2014

SysQueryRangeUtil, dynamic dates in Reports

There are multiple reports in Dynamics AX which you run on a daily or weekly basis and where you need to change the date range every time you run the report. For instance, you want to see your turnover of the past week on every monday. That means that every monday, you need to change your date range.

There a clever query utility which let's you create dynamic ranges. 

The different options are (today's date is 25-02-2014): 

  • (dayRange(-30,0)) - Results in a date range for the last 30 days: "26-01-2014".."25-02-2014"
  • (day(-1)) - Results in yesterday's date: 24-02-2014
  • (day(0)) - Results in today's date: 25-02-2014 
  • (day(1)) Results in tomorrow's date: 26-02-2014
  • (greaterThanDate(2)) - Results in every date after today plus 2: > 27-02-2014
  • (lessThanDate(-1)) - Results in every date of today minus 1: < 24-02-2014
  • (monthRange(0,2)) - Results in first day till the last day of the month's choosen (0 = current month): "01-02-2014".."30-04-2014"
  • (yearRange(-1,-1)) - Results in first day till the last day of the chosen year: "01-01-2013".."31-12-2013"

Now, how to apply this in a report. In a date field, simply key in the rangetype you need. Below you see the range (dayRange(-30,0)). The result will be that every day you run this report, it will display the transactions of today minus 30 days up until today. This way, you can set reports like this to run in Batch and to sent them periodically to a fileshare or e-mail them without having the need to always having to change the date range. 



To see the results in a job or to play around with the different date options, you can copy and use the job below:
static void JLH_sysQueryRangeUtil(Args _args)
{
    str         dateRange;
    ;

    // In the following date examples, date of today (25-02-2014) is taken

    dateRange = sysQueryRangeUtil::dayRange(-30,0); // Results in a date range for the last 30 days: "26-01-2014".."25-02-2014"
    info(strfmt(dateRange));

    dateRange = sysQueryRangeUtil::day(-1); // Results in yesterday's date: 24-02-2014
    info(strfmt(dateRange));

    dateRange = sysQueryRangeUtil::day(1); // Results in tomorrow's date: 26-02-2014
    info(strfmt(dateRange));

    dateRange = sysQueryRangeUtil::greaterThanDate(2); // Results in every date after today plus 2: > 27-02-2014
    info(strfmt(dateRange));

    dateRange = sysQueryRangeUtil::lessThanDate(-1); // Results in every date of today minus 1: < 24-02-2014
    info(strfmt(dateRange));

    dateRange = sysQueryRangeUtil::monthRange(0,2); // Results in first day till the last day of the month's chosen (0 = current month): "01-02-2014".."30-04-2014"
    info(strfmt(dateRange));

    dateRange = sysQueryRangeUtil::yearRange(-1,-1); // Results in first day till the last day of the chosen year: "01-01-2013".."31-12-2013"
    info(strfmt(dateRange));

}