x++ code to create business event in d365 f&o
A custom business event requires three parts.
- A contract class that defines the details of the message to be sent.
- A Business Event class, that maps the data from the D365 object to the contract class.
- The chain of command method that creates the business event during the appropriate operation.
Create The Contract Class:
/// <summary>
/// The data contract for a <c>BAS_SalesOrderBusinessEventContract</c>.
/// </summary>
[DataContract]
class BAS_SalesOrderBusinessEventContract extends BusinessEventsContract
{
private SalesIdBase salesId;
private LegalEntityDataAreaId legalEntity;
private String50 salesStatus;
/// <summary>
/// Creates a <c>rsmPackSlipPostedBusinessEventContract</c> from a <c>CustInvoiceJour</c> record.
/// </summary>
/// <param name = "_custInvoiceJour">A <c>CustInvoiceJour</c> record.</param>
/// <returns>A <c>rsmPackSlipPostedBusinessEventContract</c>.</returns>
public static BAS_SalesOrderBusinessEventContract newFromSalesTable(SalesTable _salesTable)
{
var contract = new BAS_SalesOrderBusinessEventContract();
contract.initialize(_salesTable);
return contract;
}
private void initialize(SalesTable _salesTable)
{
CustParameters custParameters = CustParameters::find();
salesId = _salesTable.SalesId;
salesStatus = enum2Symbol(enumNum(SalesStatus), _salesTable.SalesStatus);
}
private void new()
{
}
[DataMember('SalesId'), BusinessEventsDataMember("SalesId")]
public SalesIdBase parmSaleOrderId(SalesIdBase _salesId = salesId)
{
salesId = _salesId;
return salesId;
}
[DataMember('SalesStatus'), BusinessEventsDataMember("SalesStatus")]
public String50 parmSalesStatus(String50 _salesStatus = salesStatus)
{
salesStatus = _salesStatus;
return salesStatus;
}
}
Create The Business Event Class:
The business event class maps the data from the D365 object to the business event contract class. In our case, we will map data from the salesTable record to our contract class.
In Visual Studio, create a new class and paste the following code.
/// <summary>
/// Packing slip posted business event.
/// </summary>
[BusinessEvents(classStr(BAS_SalesOrderBusinessEventContract), 'AccountsReceivable::SalesOrderBusinessEvent', 'AccountsReceivable:SalesOrderBusinessEventaDescription', ModuleAxapta::SalesOrder)]
public final class BAS_SalesOrderBusinessEvent extends BusinessEventsBase
{
private SalesTable salesTable;
private SalesTable parmSalesTable(SalesTable _salesTable = salesTable)
{
salesTable = _salesTable;
return salesTable;
}
/// <summary>
/// Creates a <c>rsmPackSlipPostedBusinessEvent</c> from a <c>CustInvoiceJour</c> record.
/// </summary>
/// <param name = "_custInvoiceJour"> A <c>CustInvoiceJour</c> record.</param>
/// <returns>A <c>rsmPackSlipPostedBusinessEvent</c>.</returns>
public static BAS_SalesOrderBusinessEvent newFromSalesTable(SalesTable _salesTable)
{
BAS_SalesOrderBusinessEvent businessEvent = new BAS_SalesOrderBusinessEvent();
businessEvent.parmSalesTable(_salesTable);
return businessEvent;
}
private void new()
{
}
[Wrappable(true), Replaceable(true)]
public BusinessEventsContract buildContract()
{
return BAS_SalesOrderBusinessEventContract::newFromSalesTable(salesTable);
}
}
Call The Business Event :
When the sales order packing slip is posted, we will call the business event.
First we need to find the correct spot in code where this action takes place. Fortunately I have already found this place. It is in the method endPost in the class SalesPackingSlipJournalPost.
In Visual Studio, create a new class, and paste in the following code.
[ExtensionOf(classStr(SalesPackingSlipJournalPost))]
final class BASSalesOrderPosting_Extension
{
protected void endPost()
{
next endPost();
// Send PackSlipPostedBusinessEvent
if(chainFormLetterContract.parmVersioningUpdateType() == VersioningUpdateType::Initial)
{
// Pack Slip posted
BAS_SalesOrderBusinessEvent::newFromSalesTable(salesTable).send();
}
}
}
Activate The Custom Business Event
Finally go to System administration > Setup > Business events > Business events catalog
On the form, click on Manage > Rebuild business event catalog. This will cause the system to see your newly created custom business event and add it to the grid shown on this form.
Comments
Post a Comment