Working with utcDateTime Fields in Select Statements in Dynamics AX 2012

By Bill Thompson | June 23, 2015

Working with utcdatetime fields in select statements in dynamics ax 2012

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

Recommended Reading:

5.27.22 Dynamics GP

How to Run Drop Ship Sales in Dynamics GP

Need to Invoice the Customer before you get the invoice from the vendor? This The freight forwarder has contacted you […]

Read the Article

New Features in Dynamics 365 Business Central 2022 Wave 1 Release – Protecting G/L Accounts From Being Deleted

There are a lot of exciting New Features in Dynamics 365 Business Central 2022 Wave 1 Release to get excited […]

Read the Article
5.25.22 Financials

Rounding Numbers in Power Automate

  If you’ve ever tried to round numbers in Power Automate, you have probably already run into the issue there […]

Read the Article

Manage U.S. Use Tax on Purchase Orders in Dynamics 365 Finance and Operations

  Managing sales tax requirements on your business purchase can be complicated, but Dynamics 365 Finance and Operations can help […]

Read the Article
5.19.22 Dynamics CRM

How to Write a Great Support Ticket in the Stoneridge Support Portal

Submitting a support ticket through the Stoneridge Support Portal is a quick and effective way to get assistance for any […]

Read the Article

Managing Your Business Through Uncertain Times Using Dynamics 365 Finance and Operations

  Dynamics 365 Finance and Operations (F&O) can help you make informed decisions on how to move your business forward. […]

Read the Article
5.13.22 Power Platform

Using Power BI Object Level Security

  The following article will demonstrate how to use Power BI Object Level Security to disable column data based on […]

Read the Article
5.12.22 Dynamics CRM

How to Use the Stoneridge Support Portal

Stoneridge Software’s support portal is an intuitive and useful function that makes it easy for you to access resources to […]

Read the Article
5.6.22 Dynamics GP

Dynamics GP Transaction Removal: Purchase Orders

  Are you having performance issues with Purchase Orders?  Do you find that there are old Purchase Orders on your […]

Read the Article

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!

X