Azure Function SQL Trigger: how to use it (and why it can be useful in your Business Central projects)

This post comes as a follow-up of my “Serverless Integrations for Dynamics 365 Business Central” training I’ve done yesterday in Microsoft House in Milan.

During the training, I’ve quickly mentioned the new Azure SQL Triggers support for Azure Functions.

With this feature, an Azure Function can be automatically triggered by data changes in an Azure SQL, Azure SQL Managed Instance and SQL Server 2016-2022 database. This is a really useful scenario for handling event-driven applications and integrations between an external SQL database and your Dynamics 365 Business Central online environment.

Just to give you a quick scenario, imagine to have an external application for a B2B shop and this application saves Sales Orders in a SQL database backend (hosted on-premises or on Azure SQL). Every time a sales order is saved in the B2B application, you need to pass it to Dynamics 365 Business Central.

How can you do that in real-time, ensuring the maximum efficiency?

Possible solution: you can use the new Sql Trigger Binding for Azure Function!

How to use Sql Trigger Azure Functions?

To explain how to use this new trigger for Azure Function, I’ve created a SQL database and on this database I’ve created a table called B2BOrders:

The Azure SQL trigger uses SQL change tracking functionality to monitor a SQL table for changes and trigger a function when a row is created, updated, or deleted. To have the trigger working correctly, you need to enable change tracking in the database and in the table you need to monitor.

You can do that with the following T-SQL:

ALTER DATABASE YOURDATABASE
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);

ALTER TABLE YOURTABLE
ENABLE CHANGE_TRACKING;

Now let’s create a new Azure Function project with your preferred development tool (here I’ve used Visual Studio Code), select the runtime (.NET 8 Isolated LTS) and when prompted for selecting a function template, change the template filter (if needed) in order to have the SqlTriggerBindingIsolated template visible):

The template asks you other questions (like the name of the connection string for your SQL database and the name of the table to monitor for changes) and at the end it creates a skelethon of a function like the following:

Let’s change the Azure Function code like the following:

In this code, I’ve created a class with the same structure as my SQL table for handling the data I need to retrieve when a data change happens. Then in the Azure Function trigger’s code I simply detect the type of data operation occurs on the SQL table and I print the related information (data change).

In a real-world application (like in the scenario previously described) you need to retrieve the changed item (change.Item object in my C# code) and pass it to Dynamics 365 Business Central accordingly.

To test the Azure Function locally, you need to insert the SQL connection string in the local.settings.json file and you also need to fill (if not present) a parameter called WEBSITE_SITE_NAME:

Let’s test the trigger…

To test if the Azure Function SQL trigger works, let’s insert a record into the B2BOrders table:

When the record is inserted, the Azure Function is automatically triggered and you have the inserted record’s details (the trigger detects an INSERT operation):

Now let’s modify the amount field of the previously inserted record (chaning it to 500):

When the record is changed, the Azure Function is immediately triggered, the MODIFY operation is detected and you have the details of the modified record:

Now let’s delete the record on the B2BOrders table:

Again, the Azure Function is immediately triggered, the DELETE operation is detected and you have the details of the deleted record:

Conclusion

With this recently released SQL trigger support for Azure Functions, you can create fully serverless event-driven applications that permits you to detect changes in a SQL database and do actions in another system (like Dynamics 365 Business Central). And this can be a great solution for many integration scenarios.

In order to work, remember that the trigger needs to have read access on the table being monitored for changes and to the change tracking system tables. Scaling of the Azure Function accordingly to the data changes on SQL is handled by the function runtime.

Original Post https://demiliani.com/2025/05/08/azure-function-sql-trigger-how-to-use-it-and-why-it-can-be-useful-in-your-business-central-projects/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
June 2025
MTWTFSS
       1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30       
« May   Jul »
Follow
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...