---
title: "Business events in Dynamics 365 Finance &amp; Operations and Microsoft Azure Service Bus"
canonical: https://ontargit.com/business-events-and-microsoft-azure-service-bus/
date: 2021-09-15 09:51:11
description: "Business events in Microsoft Dynamics 365 Finance &amp; Operations and Microsoft Azure Service Bus for safety purpises."
keywords: "Business events"
categories: [Tech article, Azure, ERP]
---

## Business events

Business event is a mechanism that gives you an ability to take actions outside Dynamics 365 Finance and Operations apps in response to a business event that occurs in Finance and Operations. You can find **all the information about Business events** [here](https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/business-events/home-page#prerequisites).**
Additionally, if you are looking for some **code samples** you can visit [this](https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/business-events/business-events-dev-doc#implementation) page.

Now we are going to take a look at an example of interaction between business event in Finance and Operations and Azure Service Bus Queue.

## Business events and Azure Service Bus

For the security reasons Microsoft recommends storing connection strings outside your applications. Therefore, they provide an Azure Key Vault drive for such purposes.

All you need is to give your application the correct access to the keys, secrets, certificates.

Here is a list of the actions that you must execute to create **Azure Service Bus Queue** and connect Business event to it:

- __

1. Create a new Service Bus namespace.

- __

2. Create a new Service Bus Queue.

- __

3. Create a new key vault to store the Service Bus key.

- __

4. Register an Azure app that has permission to access the key vault.

- __

5. Configure a Business Events endpoint.

- __

6. Consume the business event.

## Create a new Service Bus namespace

- __

1. Go to the Azure portal.

- __

2. Select All services** &gt; **Integration** &gt; **Service Bus**.

_

- _

3. Select **Add** to create a new Service Bus namespace and set the parameters. Select the **Standard **pricing tier. You can create a new resource group as a container for your lab, or you can use an existing resource group.

**

_**NOTE**_

If you select the **Basic** pricing tier, you can create only queues. To create topics, you must select the **Standard** pricing tier.

_

Also, you should specify valid and readable Namespace name for your Service Bus.

Then press Review+Create button and wait for the deployment of the resource.

## Create a new Service Bus queue

- _

1. In the Azure portal, select the Service Bus that you just created, and then create a new queue.

_

- _

2. Go back to the blade for your Service Bus and create/configure a new shared access policy to send events. Only the Send** policy is required to send events to the Service Bus queue.

_

- _

3. Select the new **Send** policy, and then copy and save the **Primary Connection String** value. You will use this value later.

_

## Enhance your project delivery with OntargIT partner services

We are not a recruitment organization that just hires and resell resources. Instead we are [**service-oriented partner** ](https://ontargit.com/en/partner-services-oit/)focusing on long term collaboration with customers and development of our teams. We obtain expertise which we’ve got by providing implementation consulting  to end customers for many years, and great team synergy we’ve built by doing so.

[Free consultation_](https://ontargit.com/en/ontargit-contacts/)

## Create a new key vault

- __

1. In the Azure portal, select **All services **&gt; **Security** &gt; **Key vaults**.

_

- _

2. Create a new key vault in your resource group and set the default parameters. Click review + create. Wait till resource is deployed.

_

- _

3. Select **Overview**, then copy and save the **Vault URI **value for the key vault. You will use this value later.

_

- _

4. Select **BETestServiceBusKeyVault** &gt; **Secrets** &gt; **Generate/Import**. Enter a name for your secret and paste the Service Bus connection string that you saved earlier.

_

- _

5. Select **Create.**

_

## Register a new application

In this procedure, you will register a new application with Azure AD, and give it read and retrieve access to key vault secrets. Finance and Operations will then use this application to retrieve Service Bus secrets.

- _

1. In the Azure portal, select **All services &gt; Security &gt; Azure Active Directory**.

_

- _

2. Select **App registrations (preview) &gt; New registration**, and enter a name for your application. Select **Register**.

_

- _

3. Select the new application, and then select **Certificates &amp; secrets &gt; New client secret.** Enter a name for your secret and set the secret so that it never expires. Then select **Add.**

_

- _

4. Copy and save your new secret. You will use it later.

_

- _

5. Select **Overview**, and copy and save the application ID. You will use this value later.

_

- _

6. Select **All services &gt; Security &gt; Key vaults.**

- __

7. Select the key vault that you created earlier, and then select **Access policies &gt; Add new**.

_

- _

8. On the **Principal** blade, select your new registered application. Select the check all boxes for the **Key management operations, Secret management operations** and **Certificate permissions**.

_

- _

9. Save your new access policy.

_

## Configure a Business Events endpoint

- _

1. Sign in to the application and go to **System administration &gt; Setup &gt; Business events**.

- __

2. Select **Endpoints**.

- __

3. Select **New**.

- __

4. Select **Azure Service Bus Queue.**

- __

5. Select **Next.**

_

- _

6. Set the required parameter values.

_

## Consume a business event

In the current scenario we will just call send method for the business event directly from runnable class:

- _

1. Select the business event catalog and look for **Invoice journal** business event.

- __

2. Activate the business event for USMF company.

_

After you activate a business event that uses the new Service Bus endpoint, the application sends a test message to verify that the configuration is accurate and to cache the connection.

- _

3. To verify that the test message has been received, in the Azure portal, select your **created queue**, and check for a message in it. Verify that the message count for the queue shows a value of at least **1**. If it doesn’t, wait for the batch job to pick up your message.

_

- _

4. Select **All services &gt; Integration &gt; Logic Apps.**

_

- _

5. Create a new logic app in your resource group.

- __
6. After your Logic Apps resource has been created, select the option to create a blank logic app.

- __

7. Search for **Service Bus** and select it.

- __

8. Select the trigger that is named **When a message is received in a queue (auto complete).**

_

If you want to change connection for a different Service Bus queue, click Change Connection.

- _

9. Select **New step** to add a new action.

- __

10. Search for the **Parse Json** data operation. This step is required so that the message can be parsed by using the schema of the data contract.

The body content that is received from the Service Bus is encoded into base64 format. Therefore, you must transform it to string format before the JavaScript Object Notation (JSON) payload can be parsed.

- __

11. Click in the **Content **field, and then, in the pane that appears, on the **Expression** tab, enter the following expression: **Base64ToString()**

_

Next, you must enter the schema of the contract that is received from the application. The application only provides a sample payload. However, you can use a capability of Azure Logic Apps to generate a schema from a payload.

- _

12. Select your event in the business event catalog, and then select the **Download schema** link. Open the text file that is downloaded and copy the contents.

_

- _

13. Depending on the quality of your sample payload, your generator won’t be able to distinguish between an integer and a real value, especially if the real value is provided as a whole number in the sample payload. Review the schema that is generated and determine whether you must change a field of the **integer** data type to the **number** data type. (In JSON, the **number** data type represents real values.)

_

- _

14. Search for the **send email** action, and then sign in to your Microsoft 365 account.

- __

15. Fill in the message with the required fields.

_

- _

16. Save your logic app.

- __

17. Trigger the business event by calling send method from code. Then verify that the logic app runs, and that you receive an email that includes customer payment details.

Copy to Clipboard
Syntax Highlighterclass BusinessEventRunner

{

 /// 

 /// Runs the class with the specified arguments.

 /// 
 /// The specified arguments.
 public static void main(Args _args)

 {

 CustInvoiceJour custInvoiceJour;

 select firstonly crosscompany custInvoiceJour;

 CustomBusinessEvent::newFromCustInvoiceJour(custInvoiceJour).send();

 }

}

__

### Article was prepared by:

VolodymyrLysenko

Software Developer, OntargIT

[](https://www.linkedin.com/in/volodymyrlysenko/)