Dynamics 365 Business Central: API query type (Develop a custom API using Query)

Dynamics 365 Business Central

Hi, Readers.
Yesterday I saw a question in Business Central forum. He created a Query object and then used it in a page API. Similar to what we discussed below.
Dynamics 365 Business Central: Using the query as a data source for a page (Query.Open Method)

This approach is not impossible, but if we just want to make the query public, we can consider other better ways. Let’s see more details.

With Business Central you can create Connect apps. Connect apps establish a point-to-point connection between Dynamics 365 Business Central and a 3rd party solution or service and is typically created using standard REST API to interchange data. Any coding language capable of calling REST APIs can be used to develop your Connect app. Microsoft has prepared many standard APIs for us. But this page type can’t be extended by creating a page extension object. Instead, you must create a new API by adding a page object. More details: Dynamics 365 Business Central: Can we extend standard APIs? – Download Standard Source Code for APIs

Here are two simple examples:
Dynamics 365 Business Central: How to use Standard/Custom API Bound Actions
Dynamics 365 Business Central: How to run AL procedure in Power Automate (Add custom execute actions)

If you need to generate a web service endpoint that joins data between different tables, then consider using an API query object. This type of API cannot be used to display data in the user interface and data can only be read (not updated). For example, Value Entries (Query 266) in Web Services.

In Postman:
Endpoint: Get

https://api.businesscentral.dynamics.com/v2.0/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Sandbox250/ODataV4/Company('Demo')/ValueEntries

PS: Using OAuth to connect Business Central APIs and Web Service in Postman

This way uses OData V4 access instead of API. Please note that they have performance differences, and API has better performance. Next is the focus of this post. In addition to API Page, do you know API Query?
Same as Page object, you can set the QueryType Property in Query object.
QueryType Property: Sets the type of query to create.

ValueAvailable or changed withDescription
Normalruntime version 1.0It is a normal query.
APIruntime version 1.0The query is of the type API.

When QueryType is set to API, like Page object, you need to set API related properties, such as APIVersion, EntityName, EntitySetName, APIPublisher and APIGroup.

Let’s look at a simple example.

In Postman:
Endpoint: Get

https://api.businesscentral.dynamics.com/v2.0/7e47da45-7f7d-448a-bd3d-1f4aa2ec8f62/Sandbox250/api/zy/zygroup/v2.0/companies(f22115b5-4b80-ef11-ac21-6045bd6483b3)/zypurchaseOrders

In Power Automate:

Test code: Github

query 50100 "ZY Purchase Order Query"
{
    Caption = 'ZY Purchase Order Query';
    OrderBy = Descending(Buy_from_Vendor_No_);
    QueryType = API;
    APIVersion = 'v2.0';
    EntityName = 'zypurchaseOrder';
    EntitySetName = 'zypurchaseOrders';
    APIPublisher = 'zy';
    APIGroup = 'zygroup';


    elements
    {
        dataitem(Purchase_Header; "Purchase Header")
        {
            column(Buy_from_Vendor_No_; "Buy-from Vendor No.")
            {
            }
            column(Buy_from_Vendor_Name; "Buy-from Vendor Name")
            {
            }
            column(Order_Date; "Order Date")
            {
            }
            column(Currency_Code; "Currency Code")
            {
            }
            column(Amount_Including_VAT; "Amount Including VAT")
            {
            }
            dataitem(Purchase_Line; "Purchase Line")
            {
                DataItemLink = "Document Type" = Purchase_Header."Document Type",
                "Document No." = Purchase_Header."No.";
                column(No_; "No.")
                {
                }
                column(Description; Description)
                {
                }
                column(Quantity; Quantity)
                {
                }
                column(Amount; Amount)
                {
                }
                dataitem(Item; Item)
                {
                    DataItemLink = "No." = Purchase_Line."No.";
                    column(Inventory; Inventory)
                    {
                    }
                }
            }
        }
    }
}

Very simple, so when you need a custom API to display data without modifying it, you may prefer to use the Query object. Give it a try!!!😁

PS:
1. API query type can also be opened in the UI.

2. Dynamics 365 Business Central Online: Endpoints for the APIs (V1.0, V2.0, Beta, custom, automation……)

APIsEndpoint
API (v1.0)https://{businesscentralPrefix}/v2.0/{Tenant id}/{Environment id}/api/v1.0/companies({id})/
API (v2.0)https://{businesscentralPrefix}/v2.0/{Tenant id}/{Environment id}/api/v2.0/companies({id})/
API (Beta)https://{businesscentralPrefix}/v2.0/{Tenant id}/{Environment id}/api/{APIPublisher}/{APIGroup}/beta/companies({id})/
custom APIshttps://{businesscentralPrefix}/v2.0/{Tenant id}/{Environment id}/api/{APIPublisher}/{APIGroup}/v2.0/companies({id})/{EntitySetName}
automation APIshttps://{businesscentralPrefix}/v2.0/{Tenant id}/{Environment id}/api/microsoft/automation/v2.0/companies({id})
OData Web Serviceshttps://{businesscentralPrefix}/v2.0/{Tenant id}/{Environment id}/ODataV4/companies({name})/{Service Name}

3. API query type

Extending API pages and queries isn’t currently possible in Business Central.

END

Hope this will help.

Thanks for reading.

ZHU


コメント

Copied title and URL