The ideal use case of business event is when you need to take some action outside of D365FO from business event response, and it is useful in case of lightweight payload. In this example the downloadable file URL generated in D365FO will be used by external system for further business purpose.
Business use case – The FO data needs to be written into a text file, the file needs to be uploaded on blob storage and the generated URL should to be shared via business event as part of integration to external system.
This post shows how to implement the business event using X++ code.
We are taking a simple business case as an example, just to understand the implementation, the classes can be updated as per the requirements, specifically the contract class methods and the trigger points.
High level steps to implement business event are –
- Build the business event contract class.
- Build the event class.
- Implement the logic to trigger the business event from FO.
Implement contract class as below. It must be extended with base class ‘BusinessEventsContract’.
A class must be decorated with [DataContract] attribute. Implement the param method that will access our URL.
The param method is decorated with DataMember and BusinessEventsDataMember, those will be appeared on ‘Business events catalog’ form as Field Name and Field Label respectively.
Similarly, implement initialize() and constructor method.
Next is business event class. Same as contract class – this class must be extended with business event base class. (BusinessEventsBase)
_downloadableURL variable in new method is used to initialize business event contract.
You can decorate the class with Business event label and description that will be appeared on ‘Business event dialog’ form as ‘Name’ and ‘Description’.
Implement the buildContract methods and other methods as shown. buildContract method will be called when the business event is activated. We can activate it from ‘Business event catalog’ form or if you are testing it through power automate, it will activate automatically for you when you save the flow with business event details. You can verify the new endpoint has created on Business event catalog form.
Business event catalog form, highlighted details are the labels that we've created in above classes.
Now, as we have business event classes ready, we can write a class to trigger it.
Create one new class and add below method. createFile() will create a new file and return URL of temporary blob storage. I've used sample text here in fileData variable, whereas you can generate the text file string as per the required logic and set to it.
The business will be triggered only when the event is activated.
Below method is implemented to create a file, upload it to temporary blob storage and return the generated URL back to process() method.
Standard class FileUploadTemporaryStorageStrategy is helpful to create this.
No comments:
Post a Comment