Dynamics 365 FO Integration using Business Events

This blog describes a method to use Azure Integration with Dynamics 365 FO Business Events. The Dynamics 365 FO Business Events can send events/trigger/notification to external applications such as Azure Integrations, which can use this trigger to handle specific integration or business process scenarios.

The Events existed in Finance and Operations were previously confined to use within Finance and Operations. The new capability provides a framework that will allow business processes in Finance and Operations to capture business events as business processes are executed and send the events to an external system or application.

More about business event can be found here

Business events provides a perfect integration scenario when an event occurs in D365FO and requires this information to be passed on to ThirdParty systems.

These business event can be used by

  • Azure Service Bus
  • Azure Logic Apps
  • Microsoft Flow
  • Azure Functions
  • HTTPS Trigger

Since these events happen in the context of business processes, they are called business events that enable business process integration.
External business processes will subscribe to specific business events from Finance and Operations to get notified when they occur. The business events can also be consumed as “triggers” in the Finance and Operations connector.

A Dynamics 365 FO Integration usecase scenario

Use case: Trigger a Third party application when a Vendor Record is created

In high level what i am trying to achieve is shown below. A custom business event will be created to trigger logic app to forward the trigger to Third party application.

Dynamics 365 FO Integration Design Pattern: Business Events

Creating a custom Dynamics 365 FO business event

In this demo I will create a new business event from scratch to show the steps involved in creating and consuming business event via Logic App. As a trigger data source, I will use Vendor Table (VendTable) as source of the business event. To create a custom business event, we would need following three artifacts.

  • BusinessEventsContract class
  • BusinessEventsBase class 
  • A Trigger Class to send the business Event

More information on creating business event can be found here

BusinessEventContract Class

BusinessEventsContract Class creates the business data contract class. A business event contract class extends the BusinessEventsContract class. It defines and populates the payload of the business event. The process of implementing a business event contract involves extending the BusinessEventContract class, defining internal state, implementing an initialization method, implementing a static constructor method, and implementing parm methods to access the contract state.

/// <summary>
/// The data contract for the <c>VendorCreatedBusinessEvent</c>,business events.
/// </summary>
[DataContract]
public  class DevVendorCreatedBusinessEventContract extends BusinessEventsContract
{   
    private VendAccount vendAccount;
    /// <summary>
    /// Initializes the field values.
    /// </summary>
    private void initialize(VendTable _vendTable)
    {
        vendAccount = _vendTable.AccountNum;
      
    }
    /// <summary>
    /// Creates a <c>VendorCreatedBusinessEventContract</c> from a <c>VendTable</c> record.
    /// </summary>
    /// <param name = "_VendTable">A <c>VendTable</c> record.</param>
    /// <returns>A <c>VendorCreatedBusinessEventContract</c>.</returns>
    public static DevVendorCreatedBusinessEventContract newFromVendTable(VendTable _vendTable)
    {
        var contract =  DevVendorCreatedBusinessEventContract::construct();
        contract.initialize(_vendTable);
        contract.parmVendAccount(_vendTable.AccountNum);
        return contract;
    }

    [DataMember('AccountNumber'), BusinessEventsDataMember("@Dev:AccountNumber")]
    public VendAccount parmVendAccount(VendAccount _vendAccount = vendAccount)
    {
        vendAccount = _vendAccount;

        return vendAccount;
    }
   
    private void new()
    {
    }

    public static DevVendorCreatedBusinessEventContract construct()
    {
        DevVendorCreatedBusinessEventContract retVal = new DevVendorCreatedBusinessEventContract();
        return retVal;
    }    
}
BusinessEventsBase extension

The process of implementing an extension of the BusinessEventsBase class involves extending the BusinessEventsBase class, and implementing a static constructor method, a private new method, methods to maintain internal state, and the buildContract method.

[BusinessEvents(classStr(DevVendorCreatedBusinessEventContract),
"Dev:VendorCreatedEvent","Dev:VendorCreatedEventDescription",ModuleAxapta::Vendor)]
public final class DevVendorCreatedBusinessEvent extends BusinessEventsBase
{
    private VendTable vendTable;
    private VendTable parmVendTable(VendTable _vendTable = vendTable)
    {
        vendTable = _vendTable;
        return vendTable;
    }

    private void new()
    {
        super();
    }

    public static DevVendorCreatedBusinessEvent construct()
    {
        DevVendorCreatedBusinessEvent retVal = new DevVendorCreatedBusinessEvent();
        return retVal;
    }

    [Wrappable(true), Replaceable(true)]
    public BusinessEventsContract buildContract()
    {
        return DevVendorCreatedBusinessEventContract::newFromVendTable(vendTable);
    }

    static public DevVendorCreatedBusinessEvent newFromVendTable(VendTable _vendTable)
    {
        DevVendorCreatedBusinessEvent businessEvent =  DevVendorCreatedBusinessEvent::construct();
        businessEvent.parmVendTable(_vendTable);
        return businessEvent;
    }
    
}
Sending/Triggering a Dynamics 365 FO business event

The trigger class is responsible for triggering the business event. In my use case, i would like to trigger the business event after the creating on Vendor record in the vendTable. So I will be extending the “VendTable_onInserted” method to send the business event.

public static class DevVendorCreatedBusinessEventTrigger_Extension
{
    
    /// <summary>
    ///Send the business event on vendor record creation.
    /// </summary>
    /// <param name="sender">Vendor Table</param>
    /// <param name="e"></param>
    [DataEventHandler(tableStr(VendTable), DataEventType::Inserted)]
    public static void VendTable_onInserted(Common sender, DataEventArgs e)
    {
      
        VendTable vendTable = sender;
        DevVendorCreatedBusinessEvent businessEvent = DevVendorCreatedBusinessEvent::newFromVendTable(vendTable);
        if(businessEvent)
        {
            businessEvent.send();
        }
    }
}

Activate the custom Dynamics 365 FO business event

The Business Event catalog doesnt get automatically refreshed. To refresh the Business Event catalog, go to
System Administration -> Business Event Catalog -> Manage -> Rebuild business event catalog
Once rebuild is complete, the new business event would be added to the list .

Dynamics 365 FO Integration Design Pattern: Business Events

Activate the Business Event and assign the end point to the busiess event. Once it is activated, the business event should appear in the “Active events” tab.

Consume the Dynamics 365 FO business event in Logic App

The newly created Business event would appear as the Trigger in Logic App under D365 Fin and Ops Module.

Dynamics 365 FO Integration Design Pattern: Business Events

Then the Logic app can be used to get the information about the vendor and forward it to the third party applications.

Dynamics 365 FO Integration Design Pattern: Business Events

    Published by Poojith Jain

    Poojith Jain is an Azure Architect with good experience with software design and development. He has a thorough knowledge of Azure Integration and he is passionate about solving complex and challenging problems in the field of Azure

    11 thoughts on “Dynamics 365 FO Integration using Business Events

    1. Hi,

      Great post.
      I have try on my dev box so i see the event in Dynamics catalog.
      But i don’t know how to activate it and with wich en points, my list is empty.
      Can you help me about it.

      Thanks a lot in advance.

    2. hi Poojith,
      you have nicely explained business events. you are using it when creating a vendor that means a single table will be sent. Can you tell me can I use the business event for complex business entity like creation of a worker as it has many tables associated with it. Moreover, if a worker is already created and there is an update in worker record like position or reporting manager, contact information or address etc. Do I need to send all the table buffer again.

    3. Hi,

      Thanks for all your answers,

      it’s working good and i was able to create a new business event and create a file on prem using on prem data gateway.

      only thing left now is to find a way to access an “MS Access DB”….

      Gill

    4. Hi Gill,
      Did you expose the VendorAccount number in the DevVendorCreatedBusinessEventContract class as DataMember ?
      [DataMember(‘AccountNumber’), BusinessEventsDataMember(“@Dev:AccountNumber”)]
      public VendAccount parmVendAccount(VendAccount _vendAccount = vendAccount)
      {
      vendAccount = _vendAccount;

      return vendAccount;
      }

      Once you expose it, then you can access them from Logic Apps

    5. Hi,

      i am trying your example – how to you get the vendor account number from the business event – i cant see ant dynamic content ?

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

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

    %d bloggers like this: