Business Event Create and Implement – Dynamics 365 For Finance and Operations – X++

Introduction:

Business event introduced in Microsoft Dynamics 365 for Finance and operations to trigger the business process based events and to talk to external systems and by default Microsoft provide some default business(example VendorInvoicePostedBusinessEvent to understand how it works.

Scenario:

Whenever sales invoice is posted then business event should be trigger only in case of specific item. To implement this below is the procedure.

Implementation steps:

3 steps for every business event

1. Build the contract
2. Build the event
3. Code to send the event

Two Classes involves:

Business Event -This class extends the BusinessEventBase , supports constructing the business event, building payload and sending business event

Example of BusinessEvent Class (SalesOrderCreation, update, Delete in that case the BusinessEvent class name should be like SalesOrderCRUDOperation) same goes for Purchase order etc

Business Event Contract – This class extends the BusinessEventsContract class. it defines the payload of business event and allows for population of contract at runtime.

 

 

Implementation :

Example is on Creation of Customer Invoice Transactions return specific item number,name, quantity and amount
Define the contract class
[DataContract]
public final class CustomerInvoiceTransactionItem90Contract extends BusinessEventsContract
{
    private ProdName productName;
    private ItemId itemId;
    private int quantity;
    private amount itemAmount;
    /// <summary>
    /// Initialise method
    /// </summary>
    /// <param name = “_invoiceTrans”> </param>
    private void initialize(CustInvoiceTrans _invoiceTrans)
    {
        CustInvoiceJour custInvoiceJour =                CustInvoiceJour::findFromCustInvoiceTrans(_invoiceTrans.SalesId,_invoiceTrans.InvoiceId,_invoiceTrans.InvoiceDate,_invoiceTrans.numberSequenceGroup);
SalesTable salesTable = SalesTable::find(_invoiceTrans.SalesId);
CustTable custTable = CustTable::find(salesTable.CustAccount);
        // get the variable values
        productName =    EcoResProduct::findByProductNumber(_invoiceTrans.ItemId).productName();
 itemId = _invoiceTrans.itemId;
 quantity = _invoiceTrans.quantity;
 itemAmount = _invoiceTrans.LineAmount;
}
    /// <summary>
    ///
    /// </summary>
    /// <param name = “_invoiceLine”></param>
    /// <returns></returns>
    public static CustomerInvoiceTransactionItem90Contract newFromInvoice(CustInvoiceTrans _invoiceLine)
    {
           CustomerInvoiceTransactionItem90Contract contract = new
                                                                              CustomerInvoiceTransactionItem90Contract();
           contract.initialize(_invoiceLine);
           return contract;
    }
    private void new()
    {
    }
    [DataMember(‘ProductName’), BusinessEventsDataMember(“Product Name”)]
    public ProdName parmProdName(str _productName = productName)
    {
        productName = _productName;
        return productName;
    }
    [DataMember(‘itemId’), BusinessEventsDataMember(“Item Number”)]
    public str parmSerialNumber(str _itemId = itemId)
    {
        itemId = _itemId;
        return itemId;
    }
    [DataMember(‘quantity’), BusinessEventsDataMember(“Quantity”)]
    public str parmLineId(str _quantity = quantity)
    {
        quantity = _quantity;
        return quantity;
    }
    [DataMember(‘itemAmount’), BusinessEventsDataMember(“itemAmount”)]
    public str parmfoPartyType(str _itemAmount = itemAmount)
    {
        itemAmount = _itemAmount;
        return itemAmount;
    }
}
Define the Business Event Class
 [BusinessEvents(classStr(CustomerInvoiceTransactionItem90Contract),
    “Customer Invoice Transaction”,
    “This business event is triggered when an invoice is created.”,
    ModuleAxapta::SalesOrder)]
     public class CustInvoiceTransCreatedBusinessEvent extends BusinessEventsBase
 {
     CustInvoiceTrans custInvoiceTrans;
      private custInvoiceTrans parmInvoiceTrans(CustInvoiceTrans _custInvoiceTrans =              custInvoiceTrans)
     {
         custInvoiceTrans = _custInvoiceTrans;
         return custInvoiceTrans;
     }
     private void new()
     {
     }
     [Wrappable(true), Replaceable(true)]
     public BusinessEventsContract buildContract()
     {
         return      CustomerInvoiceTransactionItem90Contract::newFromInvoice(custInvoiceTrans);
     }
    static public CustInvoiceTransCreatedBusinessEvent    newFromInvoice(CustInvoiceTrans _custInvoiceTrans)
     {
         CustInvoiceTransCreatedBusinessEvent event = new       CustInvoiceTransCreatedBusinessEvent();
        event.parmInvoiceTrans(_custInvoiceTrans);
         return event;
     }
}
Code to invoke the event : below is the code that invoke the business event classes that we define above
 [DataEventHandler(tableStr(CustInvoiceTrans), DataEventType::Inserted)]
    public static void CustInvoiceTrans_onInserted(Common sender, DataEventArgs e)
    {
        CustInvoiceTrans invoiceTrans = sender as CustInvoiceTrans;
        boolean isEvent = BusinessEventsConfigurationReader::isBusinessEventEnabled(classStr(CustInvoiceTransCreatedBusinessEvent));
        if (isEvent)
        {
            if(invoiceTrans.itemid==’item90′)
            {
                CustInvoiceTransCreatedBusinessEvent::newFromInvoice(sender).send();
            }
        }
    }
  • After finishing the above code and rebuilding the project then you have to go business event catalogue in the system administration
  • You will find your newly created business event in the list
  • Then from your end point application register the business event , once successfully registered than you will find the end point in your business event catalog Endpoint tab
  • Activate the business event , you are ready to use the newly created business event.

 

Note: Business event can be activated only in 1 legal entity  or all , if you don’t selected any legal entity during activation then it will be activated for all else only for selected legal entity

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 )

Google photo

You are commenting using your Google 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.