kuhi
kuhi

Reputation: 699

How to create a nested web service in Business Central with Page Parts?

I've created this API page in Business Central:

page 60012 CustomerPL
{
    PageType = API;
    EntitySetName = 'customerPLs';
    EntityName = 'customerPL';
    APIPublisher = 'MyCompany';
    APIGroup = 'MyCompany';
    SourceTable = "Customer";
    InsertAllowed = false;
    ModifyAllowed = false;
    DeleteAllowed = false;

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field("Cliente"; Rec."No.")
                {
                    ApplicationArea = All;
                }
                part(partCustomerBanksPL; 60013)
                {
                    ApplicationArea = All;
                    EntitySetName = 'partCustomerBanksPLs';
                    EntityName = 'partCustomerBanksPL';
                    SubPageLink = "Customer No." = field("No.");
                }
            }
        }
    }
}

My idea is to have a Customer web service with banks, addresses, etc. in a nested json following this guide

This is the ListPart:

page 60013 partCustomerBanksPL
{
    ApplicationArea = All;
    Caption = 'Bancos clientes PL';
    PageType = ListPart;
    SourceTable = "Customer Bank Account";
    
    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("Code"; Rec."Code")
                {
                }
                field(Name; Rec.Name)
                {
                }
                field(IBAN; Rec.IBAN)
                {
                }
                field("SWIFT Code"; Rec."SWIFT Code")
                {
                }
            }
        }
    }
}

Running the GET request:

https://api.businesscentral.dynamics.com/v2.0/{{tenant_id}}/Sandbox_PL/ODataV4/Company('MyCompany')/customerPL?$expand=partCustomerBanksPL

I'm getting this error:

{
    "error": {
        "code": "BadRequest",
        "message": "Could not find a property named 'partCustomerBanksPL' on type 'NAV.customerPL'.  CorrelationId:  34fca7a0-ae62-4218-90f1-09ad53e0cfc0."
    }
}

So... How can I create a nested web service in Business Central with Page Parts like I'm trying?

EDIT: code updated to match names and camelCase

Upvotes: 1

Views: 644

Answers (2)

Tonya B.
Tonya B.

Reputation: 11

page 60013 partCustomerBanksPL also needs to be an API page.

page 60013 CustomerBankAccountAPI
{
    APIGroup = 'apiGroup';
    APIPublisher = 'publisherName';
    APIVersion = 'v2.0';
    ApplicationArea = All;
    Caption = 'customerBankAccountAPI';
    DelayedInsert = true;
    EntityName = 'customerBankAccount';
    EntitySetName = 'customerBankAccounts';
    PageType = API;
    SourceTable = "Customer Bank Account";
    
    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("code"; Rec."Code")
                {
                    Caption = 'Code';
                }
                field(name; Rec.Name)
                {
                    Caption = 'Name';
                }
                field(iban; Rec.IBAN)
                {
                    Caption = 'IBAN';
                }
                field(swiftCode; Rec."SWIFT Code")
                {
                    Caption = 'SWIFT Code';
                }
            }
        }
    }
}

Then you just need to update the part on your Customer API.

part(partCustomerBanksPL; CustomerBankAccountAPI)
{
    EntitySetName = 'customerBankAccount';
    EntityName = 'customerBankAccounts';
    SubPageLink = "Customer No." = field("No.");
}

Upvotes: 1

ChristianBraeunlich
ChristianBraeunlich

Reputation: 622

Add the APIVersion property

page 60012 CustomerPL
{
    ...
    APIPublisher = 'MyCompany';
    APIGroup = 'MyCompany';
    APIVersion = 'v1.0';
    ...

and use the following HTTP request: https://api.businesscentral.dynamics.com/v2.0/>tenant_id</Sandbox_PL/api/MyCompany/MyCompany/v1.0/companies(>companyId<)/customerPLs?$expand=partCustomerBanksPLs

EDIT: Take a look at the official MS documentation on Creating API pages: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-custom-api#to-create-api-pages-to-display-car-brand-and-car-model

Upvotes: 2

Related Questions