Dynamics 365 Business Central: be careful when using the OnCompanyOpen trigger

Today I was helping a partner on discovering performance issues on its Dynamics 365 Business Central online tenant. When analyzing the telemetry data with Application Insights, we discovered a lot of Long Running Query signals (event id RT0005) linked to the OnCompanyOpen trigger.

So strange… until analyzing its installed per-tenant extensions, we discovered an extension with a code like the following:

This extension subscribes the OnCompanyOpen trigger and then adds a custom code for logging stuffs related to the user on custom tables. Please don’t do in this way if you need that!

The OnCompanyOpen trigger is executed every time a new session is opened and the session can be of different types:

  • Web Client
  • Desktop
  • Phone
  • Tablet
  • Web Services
  • Background
  • Child

This was the real issue! Not all sessions are related to user sessions, but some of them are related to web services, background tasks, job queues and so on. These types of sessions was the biggest part of sessions opened every day and that causes the performance issues. To fix the code, just remember to always check the client type:

Small tip, but that can save your life in the cloud 😉

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.