Working with utcDateTime Fields in Select Statements in Dynamics AX 2012

By Bill Thompson | June 23, 2015

Working with utcDateTime fields in Dynamics AX 2012 can be a bit tricky, but once you understand what is going on, it really does make sense. For example, suppose you are looking in the ProdJournalTable for records that have a journal type of ‘Reported as Finished’, and you want them to be shown for a certain date/time range. Looking at the data in the table browser, it looks like the following:

Report as Finished

So, based on the data, this looks like a quick select statement. The following code is created:

static void DateTimeSelectExample(Args _args)

{

// time variable declarations

   utcDateTime         rangeStart, rangeEnd;

date               startDate, endDate;

TimeOfDay           startTime, endTime;




// table buffer and associated variables

ProdJournalTable   prodJournalTable;







// set range variables

startDate   = 6\12\2012;       // day\month\year

endDate     = 6\12\2012;       // day\month\year




startTime   = str2time("08:30:00 am");     //GMT, NOT user time zone - this is how stored in the table

endTime     = str2time("09:00:00 am");     //GMT, NOT user time zone - this is how stored in the table




// create new datetime objects to use in range

rangeStart = DateTimeUtil::newDateTime(startDate,startTime);

rangeEnd   = DateTimeUtil::newDateTime(endDate,endTime);




    // get Productions orders that fall within the range values

    while select prodJournalTable where prodJournalTable.PostedDateTime >= rangeStart

&& prodJournalTable.PostedDateTime <= rangeEnd

&& prodJournalTable.JournalType == ProdJournalType::ReportFinished

{

info(strFmt("Production order: %1, Posted date time: %2",prodJournalTable.ProdId,datetime2str(prodJournalTable.PostedDateTime)));

}

info("Done");

}

Pretty straightforward job. BUT, when it is run, the infolog returns the following:

Infolog in Dynamics AX

Looking at the code, it appears to be correct.

The issue with this code is that Dynamics AX 2012 will convert the select statement to actually run using the datetime of the user preferred timezone, and not as it is displayed in the Table browser. The code needs to be adjusted to take this information into account when creating the ranging values. Code that will return what is desired should look like the following:

static void DateTimeSelectExample2(Args _args)

{

// time variable declarations

utcDateTime         rangeStart, rangeEnd;

date               startDate, endDate;

TimeOfDay           startTime, endTime;




// table buffer and associated variables

ProdJournalTable   prodJournalTable;







// set range variables

startDate   = 6\12\2012;       // day\month\year

endDate     = 6\12\2012;       // day\month\year




startTime   = str2time("08:30:00 am");     //GMT, NOT user time zone - this is how stored in the table

endTime     = str2time("09:00:00 am");     //GMT, NOT user time zone - this is how stored in the table




    // create new datetime objects to use in range

rangeStart = DateTimeUtil::newDateTime(startDate,startTime,DateTimeUtil::getUserPreferredTimeZone());           // apply user time zone to range

rangeEnd   = DateTimeUtil::newDateTime(endDate,endTime,DateTimeUtil::getUserPreferredTimeZone());               // apply user time zone to range




  // get Productions orders that fall within the range values

while select prodJournalTable where prodJournalTable.PostedDateTime >= rangeStart

&& prodJournalTable.PostedDateTime <= rangeEnd

&& prodJournalTable.JournalType == ProdJournalType::ReportFinished

{

info(strFmt("Production order: %1, Posted date time: %2",prodJournalTable.ProdId,datetime2str(prodJournalTable.PostedDateTime)));

}

info("Done");

}

The output from this now resembles the following:

Infolog Message

Please notice the Posted date time in the data above, and compare it to what is displayed in the table browser. Notice how the time zone adjustment has been applied to the data. This is what was causing the job to not return the expected data.

See my previous post for more on working with utcDateTime functionality.

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!