Dynamics 365 Business Central: OnValidate (Field) Trigger in Power Automate (Trigger a flow when a field is modified) – Business events

Dynamics 365 Business Central

Hi, Readers.
Today I would like to discuss a question I’ve been asked frequently. Is it possible to run OnValidate (Field) Trigger in Power Automate? The last time I saw it was in the Business Central Forum last week, Trigger a Power automate flow when a field is modified in Business central (dynamics.com).

Power Automate is a service that helps you create automated workflows (or flows) between apps and services, like Business Central. With Business Central, you’re given a license to Microsoft Power Automate. This license lets you use your Business Central data as part of a workflow in Microsoft Power Automate. You create flows and connect to your data from internal and external sources through the Business Central connector. More details: Power Automate -> Connector reference -> Dynamics 365 Business Central

For insert, modify, and delete triggers, Power Automate has the following four triggers.
When a record is changed (V3): Triggers a flow when a record is created, modified or deleted
When a record is created (V3): Triggers a flow when a record is created
When a record is deleted (V3: Triggers a flow when a record is deleted
When a record is modified (V3): Triggers a flow when a record is modified

No matter which one, it is for a record, not a field. So what should we do if we want to trigger the flow when only one field value is modified? Is it not possible to do so? This was not possible before, but there is a workaround in the current version, Business events.

Business events provide our partners and customers a mechanism for notifying and triggering their external systems (Dataverse and non-Dataverse) when actions are done on Business Central. The external systems can react and perform other actions in response. With Dataverse, partners and customers can use Power Automate to subscribe to Business Central for its business events and react on other apps in its ecosystem, such as Dynamics 365 Sales or Customer Service, and others built using Power Apps. They can also react on non-Dataverse systems, such as non-Microsoft warehouse management, fulfillment, and e-invoicing services. More details: Business events on Business Central (preview)

Simply put, Microsoft has added a new Business Central trigger in Power Automate.
When a business event occurs (V3): Triggers a flow when a certain business event occurs in Dynamics 365 Business Central.

This includes some special events, such as when a customer is locked, when an order is released, when an order is posted, etc.

Business Central catalog of business events: The following table describes the business events in Business Central catalog. These business events are readily available in version 22.2 and later but must be implemented in an extension in version 22.0 and 22.1

CategoryNameDescription
Accounts PayablePurchase credit memo postedTriggered when a purchase credit memo is posted.
Accounts PayablePurchase invoice postedTriggered when a vendor invoice is posted as part of the Procure-to-Pay process.
Accounts PayablePurchase payment postedTriggered when a vendor payment is posted as part of the Procure-to-Pay process.
Accounts PayablePurchase receipt postedTriggered when goods from a purchase order are received by the internal warehouse/external logistics company. This event can trigger Finance Department to post a purchase invoice.
Accounts ReceivableCustomer blockedTriggered when a customer is blocked for shipping/invoicing.
Accounts ReceivableCustomer unblockedTriggered when a customer is unblocked for shipping/invoicing.
Accounts ReceivableSales credit limit exceededTriggered when the credit limit for a customer is exceeded due to a posted sales invoice/changed credit limit for that customer.
Accounts ReceivableSales credit memo postedTriggered when a sales credit memo is posted.
Accounts ReceivableSales invoice postedTriggered when a sales invoice is posted as part of the Quote-to-Cash process.
Accounts ReceivableSales payment postedTriggered when a customer payment is posted as part of the Quote-to-Cash process.
Accounts ReceivableSales shipment postedTriggered when goods from a sales order are shipped by the internal warehouse/external logistics company. This event can trigger Finance Department to post a sales invoice.
OpportunitiesOpportunity activatedTriggered when an opportunity is activated as part of the Quote-to-Cash process.
OpportunitiesQuote created for opportunityTriggered when a quote is created for an opportunity as part of the Quote-to-Cash process.
OpportunitiesWinning quote converted into sales orderTriggered when a winning quote for an opportunity is converted into a sales order as part of the Quote-to-Cash process.
OpportunitiesOpportunity closed as lostTriggered when a lost opportunity is closed as part of the Quote-to-Cash process.
PurchasingPurchase order releasedTriggered when a purchase order is released to the internal warehouse/external logistics company, so they’re ready to receive goods coming their way. This trigger occurs when the Release button is clicked on Purchase Order page in Business Central.
SalesSales order releasedTriggered when a sales order is released to the internal warehouse/external logistics company, so they’re ready to pick and ship goods. This trigger occurs when the Release button is clicked on Sales Order page in Business Central.

These are defined in AL. These codes are stored in the Exclude_Business_Events extension.
PS: How to list all installed extensions/apps in your Business Central environment (Including hidden apps – “_Exclude_……”)

We can add dependencies settings in the app.json file to download its symbol file.

  "dependencies": [
    {
      "id": "6f2c034f-5ebe-4eae-b34c-90a0d4e87687",
      "name": "_Exclude_Business_Events_",
      "publisher": "Microsoft",
      "version": "24.0.0.0"
    }
  ],

For example, Sales order released:

Customer blocked:

How do we understand the standard code here? Let’s go back to Sales order released. Simply put, there are three steps.
1. Defining External Business Events

ExternalBusinessEvent(Name: Text, DisplayName: Text, Description: Text, Category: Enum, [Version: Text])
Specifies that the method is published as an external business event.

2. Subscribe to the event to specify when it needs to be triggered.

EventSubscriber(ObjectType: ObjectType, ObjectId: Integer, EventName: Text, ElementName: Text, SkipOnMissingLicense: Boolean, SkipOnMissingPermission: Boolean)
Specifies the event to which the method subscribes.

3. Put the procedure created in step 1 into the procedure in step 2.

PS: If you need more detailed instructions, you can refer to Microsoft documentation, Build and install an extension for custom business events.
Sample code provided by Microsoft:

enumextension 50101 MyEnumExtension extends EventCategory
{
   value(0; "Sales")
   {
   }
}

codeunit 50102 MyCodeunit 
{ 
   trigger OnRun()
   begin
   end; 

   [ExternalBusinessEvent('salesorderposted', 'Sales order posted', 'Triggered when sales order has been posted', EventCategory::"Sales")]
   [RequiredPermissions(PermissionObjectType::TableData, Database::"Sales Header", 'R')] // optional
   procedure SalesOrderPosted(salesOrderId: Guid; customerName: Text; orderNumber: Text)
   begin
   end;
   
   [EventSubscriber(ObjectType::Page, Page::"Sales Order", 'OnPostDocumentBeforeNavigateAfterPosting', '', true, true)] 
   local procedure OnPostDocument(var SalesHeader: Record "Sales Header"; var PostingCodeunitID: Integer; var Navigate: Enum "Navigate After Posting"; DocumentIsPosted: Boolean; var IsHandled: Boolean) 
   begin
      SalesOrderPosted(SalesHeader.SystemId, SalesHeader."Sell-to Customer Name", SalesHeader."No."); 
   end;
}

We can refer to it to do similar things, such as creating an External Business Event and then putting it in the OnAfterValidateEvent of the field. Let’s get started.

1. Create a new Event Category

PS: If you downloaded the symbol file for Exclude_Business_Events, you do not need to create it.

A member of type EnumValue with name ‘Sales’ is already defined in EnumExtension ‘External Events Category’ by the extension ‘_Exclude_Business_Events_ by Microsoft (24.4.22295.22576)’.ALAL0155

2. Create a codeunit and define a new External Business Event

3. Put it in the OnAfterValidateEvent of the Name field in the Customer table.

That’s all. After publishing the extension, return to Power Automate and you will find this new Business Event. The DisplayName of the External Business Event and the publisher of the extension are displayed here.

Test code:

enumextension 50221 EventCategoryExt extends EventCategory
{
    value(0; "Sales")
    {
    }
}

codeunit 50222 BusinessEventHandler
{
    [ExternalBusinessEvent('customernameonvalidate', 'Customer name onvalidate', 'Triggered when customer name has been validated', EventCategory::"Sales")]
    procedure CustomerNameOnValidate(customerId: Guid)
    begin
    end;

    [EventSubscriber(ObjectType::Table, Database::Customer, OnAfterValidateEvent, Name, false, false)]
    local procedure CustNameOnValidate(var Rec: Record Customer)
    begin
        CustomerNameOnValidate(Rec.SystemId);
    end;
}

Let’s look at a specific example. Create a Power Automate flow to send the modified customer name to the Teams group when the customer name is modified.

1. Search for Business Central, and click on it. Then select When a business event occurs (V3) trigger.

Select the BC environment that needs to be connected and the External Business Event that needs to be used. Company is an optional field

2. Search for Business Central, and click on it. Then select Get record (V3) action.

Select the BC environment info and the customers API that needs to be used.

Row id can be set to customerId.

This is defined in AL.

3. Search for Teams, and click on it. Then select Post message in a chat or channel action.

Set Post as, Post in and other information, and then edit the message.
PS: Dynamics 365 Business Central: How to send a message in Teams using Power Automate

PS: Any value from the Get record (V3) step can be used in the Message.

Okay, let’s do a test.

Great.

In theory, this supports any event in Business Central. More details about Events in AL.
Test video:

Very simple, give it a try!!!😁I’m sure you can find a better way to use it than I did.

Update 2024.09.25: Flows using business events may require refresh

PS:
1. This flow’s owner needs a premium license

2. Business events on Business Central is available as part of a preview release. The content and the functionality are subject to change.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL