Custom Business Event

Hello everyone, today I am going to talk about one of the interesting features of D365 F&O called "Business event".  Business event you can use to integrate with LogicApps and other Azure applications based on actions like: record created, deleted, posted etc.

In below demonstration we are going to develop custom business events for customer group which will triggered when we create new customer group. 

First, we have to create following classes:

1. Contract class- this class should extend BusinessEventsContract class and use for defining payload/fields which we want to expose while consuming the Business event.

2. Business Event class- this class should extend BusinessEventsBase class and use for constructing the business event, building payload and sending the business event request.

Contract class

/// <summary>
/// The data contract for the <c>CustomerGroupBusinessEvent</c>,business events.
/// </summary>

[DataContract]
class BECustomerContract extends BusinessEventsContract
{
    CustGroupId custgroupid;
    Name    description;
    PaymTermId  termsofpaym;
    CustClearingPeriod  clearingper;

    private void initialize(CustGroup _custgroup)
    {
        custgroupid     = _custgroup.CustGroup;
        description     = _custgroup.name;
        termsofpaym = _custgroup.PaymTermId;
        clearingper     = _custgroup.ClearingPeriod;
    }

    public static BECustomerContract newFromCustGroup(CustGroup _CustGroup)
    {
        BECustomerContract contract = new BECustomerContract();
        contract.initialize(_CustGroup);
        return contract;
    }

    private void new()
    {
    }

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

    [DataMember('CustGroupId'), BusinessEventsDataMember("Customer Group")]
    public CustGroupId parmCustGroupId(CustGroupId _CustGroupId = custgroupid)
    {
        custgroupid = _CustGroupId;
        return custgroupid;
    }

    [DataMember('Name'), BusinessEventsDataMember("Description")]
    public Name parmName(Name _Name = description)
    {
        description = _Name;
        return description;
    }

    [DataMember('PaymTermId'), BusinessEventsDataMember("Terms of Payment")]
    public PaymTermId parmPaymTermId(PaymTermId _PaymTermId = termsofpaym)
    {
        termsofpaym = _PaymTermId;
        return termsofpaym;
    }

    [DataMember('CustClearingPeriod'), BusinessEventsDataMember("Time between Invoice and payment date")]
    public CustClearingPeriod parmCustClearingPeriod(CustClearingPeriod _CustClearingPeriod = clearingper)
    {
        clearingper = _CustClearingPeriod;
        return clearingper;
    }

}

Business Event

[BusinessEvents(classStr(BECustomerContract),"CustomerGroupCreatedBusinessEvent", "CustomerGroupCreatedBusinessEvent",ModuleAxapta::Customer)]
public final class CustomerGroupCreatedBusinessEvent extends BusinessEventsBase
{
    private CustGroup CustGroup;

   static public CustomerGroupCreatedBusinessEvent newFromCustGroup(CustGroup _CustGroup)
    {
        CustomerGroupCreatedBusinessEvent businessEvent = new           CustomerGroupCreatedBusinessEvent();
        businessEvent.parmCustGroup(_CustGroup);
        return businessEvent;
    }

    private CustGroup parmCustGroup(CustGroup _CustGroup = CustGroup)
    {
        CustGroup = _CustGroup;
        return CustGroup;
    }

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

    private void new()
    {
        super();
    }

    [Wrappable(true), Replaceable(true)]
    public BusinessEventsContract buildContract()
    {
        return BECustomerContract::newFromCustGroup(CustGroup);
    }



The below code I have written on inserted event of customer group table because I want to trigger Business Event after record created.

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [DataEventHandler(tableStr(CustGroup), DataEventType::Inserted)]
    public static void CustGroup_onInserted(Common sender, DataEventArgs e)
    {
        CustGroup custgroup=sender as CustGroup;
        if (BusinessEventsConfigurationReader::isBusinessEventEnabled(classStr(CustomerGroupCreatedBusinessEvent)))
        {
            CustomerGroupCreatedBusinessEvent::newFromCustGroup(custgroup).send();
        }
    }

}


Enjoy development :)

Comments