Skip to main content

Setting the TimeZone inside of a Webhook

  • September 24, 2023
  • 1 reply
  • 168 views

Kyle Vanderstoep
Jr Varsity III
Forum|alt.badge.img+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. 

Did this topic help you find an answer to your question?

1 reply

Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • 2756 replies
  • September 25, 2023

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings