Setting the TimeZone inside of a Webhook

  • 24 September 2023
  • 1 reply
  • 103 views

Userlevel 3
Badge +1

It is standard practice for systems to communicate date/times converted to UTC. You do not know if the system you are integrating with potentially is in a different time zone. Acumatica handles this conversion under the hood for you, and you don’t have to deal with it. There can be a problem however when you integrate with a system that does not follow this practice. I am going to show you how to set the current timezone in the system within the context of a webhook, where you might be receiving datetimes in multiple different timezones from multiple different systems.

Acumatica webhooks are processed in a user contextless system, and you need to initialize the context when processing them. From Yuriy Zaletskyy’s blog post on how to write Webhook handlers, I then extended the “GetAdminScope()” method:

private IDisposable GetAdminScope()
{
var userName = "admin";
if (PXDatabase.Companies.Length > 0)
{
var company = PXAccess.GetCompanyName();
if (string.IsNullOrEmpty(company))
{
company = PXDatabase.Companies[0];
}
userName = userName + "@" + company;
}
LocaleInfo.SetTimeZone(PXTimeZoneInfo.FindSystemTimeZoneById("GMTM0800A"));
return new PXLoginScope(userName);
}

// And then inside your webhook handler:
using var adminScope = GetAdminScope();

The two critical methods are:

var timeZoneInfo = PXTimeZoneInfo.FindSystemTimeZoneById("Your Time Zone Code");
LocaleInfo.SetTimeZone(timeZoneInfo);

// You can find all the timezone codes in the PXTimeZoneInfo class.


Now, we are setting a special Time Zone for processing this webhook request. Acumatica will then transfer the datetimes correctly and store them in the database correctly converted to UTC.

I hope that perhaps this post was helpful in reducing headaches with one of your integrations. 


1 reply

Userlevel 7
Badge

Thank you for sharing this with the community @Kyle Vanderstoep!

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved