Dynamics 365 Business Central: How to run AL procedure in Power Automate (Add custom execute actions)

Dynamics 365 Business Central

Hi, Readers.
Last week I received an interesting question about whether the AL procedure can be executed in Power Automate. They want to run some programs and process only reports through Power Automate.

First let’s take a look at the standard features of Execute action in Power Automate.
Sign in to Power Automate.

Choose Create -> Automated cloud flow

Enter Flow name, then choose Skip.

To be easy to test, choose the Manual trigger a flow.

Then choose + New step

Search for Business Central, and click Business Central icon.

Choose Execute action (V3).

Execute action (V3): Executes a Dynamics 365 Business Central action.

NameKeyRequiredTypeDescription
Environment namebcenvironmentTruestringName of the Dynamics 365 Business Central environment
Company namecompanyTruestringName of the Dynamics 365 Business Central company
API categorydatasetTruestringName of the Dynamics 365 Business Central API category (also called API route)
Action nameprocedureTruestringName of action
Parameters listparametersTruedynamicInput parameters to the action

Select Environment name.

Select Company name.

Select API category. For example, v2.0.

Then you can see some standard BC actions provided by Microsoft, mainly for workflow and posting.

So, how to add our custom excute actions? Is it possible?

Next, let’s try it together.

To make it easier to understand, let’s first create a new setup page and add a record. (Not required)

Table:

table 50112 "ZY Power Automate Setup"
{
    Caption = 'ZY Power Automate Setup';

    fields
    {
        field(1; "Primary Key"; Code[10])
        {
            Caption = 'Primary Key';
            DataClassification = CustomerContent;
        }
        field(2; "Test Setup Field"; Boolean)
        {
            Caption = 'Test Setup Field';
            DataClassification = CustomerContent;
        }

    }

    keys
    {
        key(PK; "Primary Key")
        {
            Clustered = true;
        }
    }
}

Page:

page 50113 "ZY Power Automate Setup page"
{
    PageType = Card;
    Caption = 'ZY Power Automate Setup';
    ApplicationArea = All;
    UsageCategory = Administration;
    SourceTable = "ZY Power Automate Setup";
    InsertAllowed = false;
    DeleteAllowed = false;


    layout
    {
        area(Content)
        {
            group(GroupName)
            {
                Caption = 'General';
                field("Test Setup Field"; Rec."Test Setup Field")
                {
                    ApplicationArea = All;
                }
            }
        }
    }

    trigger OnOpenPage()
    begin
        Rec.Reset;
        if not Rec.Get() then begin
            Rec.Init();
            Rec.Insert();
        end;
    end;
}

Then create a new API page.

We can check this api page via Postman.

https://api.businesscentral.dynamics.com/v2.0/d8f36038-1f93-4543-affc-5dc92b6ee871/SandboxJP/api/zy/zygroup/v2.0/companies(a80a7ba3-091c-ec11-bb75-000d3a231313)/powerAutomateSetups

{
    "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/d8f36038-1f93-4543-affc-5dc92b6ee871/SandboxJP/api/zy/zygroup/v2.0/$metadata#companies(a80a7ba3-091c-ec11-bb75-000d3a231313)/powerAutomateSetups",
    "value": [
        {
            "@odata.etag": "W/\"JzQ0O3dzVnVxY1RJRlA3eHlCUGdPc0IxMTZvSXoyWkphL21rd1V5c0c3MDArMFk9MTswMDsn\"",
            "id": "be8a154c-4c4b-ec11-9f08-000d3a50caf5",
            "testSetupField": true
        }
    ]
}

Record in Business Central:

Next, add a procedure, expose the procedure using the [ServiceEnabled] attribute.
The logic example: Get item ‘1896-S’, and add ‘ZY’ after its description.

Publish the extension, and return to the Power Automate.
Select Environment name and Company name again.

Then select the new API category you specified in API object.

You can see the custom action added in API page.

Finally, fill in the id. (SystemId of the record in BC) – Required

Choose Save.

Okay, let’s test it.
Test video:

Raw inputs:

Source code:

page 50114 "ZY Power Automate Setup Entity"
{
    Caption = 'powerAutomateSetup', Locked = true;
    DelayedInsert = true;
    EntityName = 'powerAutomateSetup';
    EntitySetName = 'powerAutomateSetups';
    ODataKeyFields = SystemId;
    APIPublisher = 'zy';
    APIGroup = 'zygroup';
    APIVersion = 'v2.0';
    PageType = API;
    SourceTable = "ZY Power Automate Setup";

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field(id; Rec.SystemId)
                {
                    ApplicationArea = All;
                    Caption = 'Id', Locked = true;
                    Editable = false;
                }
                field(testSetupField; Rec."Test Setup Field")
                {
                    ApplicationArea = All;
                    Caption = 'TestSetupField', Locked = true;
                }
            }
        }
    }

    [ServiceEnabled]
    procedure ZYTestAction(var actionContext: WebServiceActionContext)
    var
        Item: Record Item;
    begin
        Item.Get('1896-S');
        Item.Description := Item.Description + 'ZY';
        Item.Modify();
        Message('Test Message');

        // Set the result code to inform the caller that an item was created.
        actionContext.SetResultCode(WebServiceActionResultCode::Created);
    end;
}

Isn’t it very convenient, give it a try.😁

PS: This method is similar to the previous Creating and Interacting with an OData V4 Bound Action.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL