Futures class in Dynamics AX

In Dynamics AX, if you want to find a date in a period, chances are you would be using the DateTimeUtil class.

Say, I want to find the date 3 months exactly from today. Assuming a month is of 30 days, I can call the DateTimeUtil::addDays() method.

So my code to get that will be,
print DateTimeUtil::date(DateTimeUtil::addDays (DateTimeUtil::utcNow(), 3 * 30));

There is another application class in Dynamics AX, which does exactly this. This is the Futures class.

Let us see this class in action.
static void FuturesClass(Args _args)
{
    Futures     future;
    int         i;
    
    // Setup a future class object with 365 days as a period
    future = new Futures(systemDateGet(), 365, PeriodUnit::Day);

    future.next();    
    
    info(strFmt("Future date after 3 months from today is %1",future.transDate()));
    
    // Setup a future class object with 1 year as a period
    future = new Futures(systemDateGet(), 1, PeriodUnit::Year);
    
    for (i=1; i<=3; i++)
    {
        future.next();
    }
    
    info(strFmt("Future date after 3 years from today is %1",future.transDate()));
    
    // Setup a future class object with 1 month as a period    
    future = new Futures(systemDateGet(), 1, PeriodUnit::Month);
    
    for (i=1; i<=3; i++)
    {
        future.next();
    }
    
    info(strFmt("Future date after 3 months from today is %1",future.transDate()));    
}
The output is
The Futures class is initialized by passing three values. The start date of the period, length of the period and the Period unit. The Period unit can be day, month or year. So when we create a new object specifying today's date as the start date, 15 as the period and day as the unit, it means each period in this instance will be 15 days long. So if I call the future.next() method, I will get a date exactly 15 days from now.

So what are the advantages of this over the DateTimeUtil class? Well, the readibility of the code is obvious. Apart from this, you dont have to worry about missing days in a period and the extra calculation needed.

For example, lets say today is 23rd August. August has 31 days, September 30 and October 31 days. So we need to add 92 days in total, which gives us 22nd November. But if I need to use the DateTimeUtil class for that, I have to do the days calculation myself whereas the Future's class handles this automatically.

The Futures class is a nice little helper class which can be helpful when calculating periods of given length in days, months or years.

I hope this post was helpful. Do send me your comments.

Thats all for today, do check back soon.

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

The field with ID '0' does not exist in table - Cause and resolution.