Develop your own Business Events
28. November 2023 Leave a comment
Business Events in Dynamics 365 Finance and Supply Chain Management can be used to notify external systems in near-time when a certain event occurs in the ERP system. Dynamics 365 F/SCM comes with a set of predefined business events. You may want to develop you own specific business events to send data to another system. Three artifacts are needed for a custom Business Event. The contract that contains the data that is sent, the Business Event and at least one trigger. Here is an example for a Business Event that triggers when a new customer is created.
Required Model Dependencies:
- Application Foundation
- Application Suite
- Directory
- Contact Person
Contract
[DataContract]
public class ERPCustomerCreatedContract extends BusinessEventsContract
{
protected Name name;
[DataMember('Name'),BusinessEventsDataMember("Customer Name")]
public Name parmName(Name _name = name)
{
name = _name;
return name;
}
public static ERPCustomerCreatedContract newFromCustTable(CustTable _custTable)
{
ERPCustomerCreatedContract contract = new ERPCustomerCreatedContract();
contract.parmName(_custTable.name());
return contract;
}
}
Business Event
[BusinessEvents(classStr(ERPCustomerCreatedContract),
'Customer Created',
'Customer Created',
ModuleAxapta::Customer)]
public class ERPCustomerCreated extends BusinessEventsBase
{
CustTable custTable;
protected void new()
{
}
public static ERPCustomerCreated newFromCustTable(CustTable _custTable)
{
ERPCustomerCreated event = new ERPCustomerCreated();
event.parmCustTable(_custTable);
return event;
}
public CustTable parmCustTable(CustTable _custTable = custTable)
{
custTable = _custTable;
return custTable;
}
[Wrappable(false), Replaceable(false)]
public BusinessEventsContract buildContract()
{
return ERPCustomerCreatedContract::newFromCustTable(custTable);
}
}
Trigger
Make sure the trigger runs after inserted not on inserting đŸ˜‰
class ERPCustTable_EventHandler
{
[DataEventHandler(tableStr(CustTable), DataEventType::Inserted)]
public static void CustTable_onInserted(Common sender, DataEventArgs e)
{
CustTable custTable = sender as CustTable;
ERPCustomerCreated::newFromCustTable(custTable).send();
}
}
Configuration
Make sure your code builds. In Dynamics 365 F/SCM open the Business Events Catalog. Rebuild the catalog to see your Business Event. Make sure you have an endpoint configured. Activate the Business Event. Create a new Customer. The Business Event will trigger and send the contract to your endpoint.
After creating a new customer, the Business Event triggers and sends the message to the configured endpoint. In my case it’s a Blob Storage in Azure. Here is the resulting JSON Message:
{
"BusinessEventId":"ERPCustomerCreated",
"BusinessEventLegalEntity":"DEMF",
"ContextRecordSubject":"",
"ControlNumber":5637166326,
"EventId":"13258F5D-9734-4EEF-8742-966C903E6896",
"EventTime":"/Date(1700741668000)/",
"EventTimeIso8601":"2023-11-23T12:14:28.5470919Z",
"InitiatingUserAADObjectId":"{2FDBF251-CB38-48ED-87CD-7515B9010431}",
"MajorVersion":0,
"MinorVersion":0,
"Name":"Test Customer",
"ParentContextRecordSubjects":[]
}
Business Events and PowerAutomate (aka. Flow)
I’ve made a video how to use Business Events in combination with Power Automate.