‘PATCH’ requests for ‘ServiceName’ of EdmType ‘Collection’ are not allowed within Dynamics 365 Business Central OData web services

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share an error handling about OData web services.

The Open Data Protocol (OData) is a web protocol that is designed for querying tabular data and provides you with an alternative to SOAP-based web services. OData builds on web technologies such as HTTP and JavaScript Object Notation (JSON) to provide access to information from different applications, services, and stores. OData uses URIs for resource identification and commits to an HTTP-based, uniform interface for interacting with resources. This commitment to core Web principles allows for OData to enable a new level of data integration and interoperability across a broad range of clients, servers, services, and tools. More details: OData Web Services

You can use OData web services to show Business Central data (Get Method), and you can update data (Patch Method) in a Business Central database using OData web services.

Recently I was asked a question, when they tried to update data (Patch Method) in a Business Central database using OData web services, they encountered the following error.

{
    “error”: {
        “code”: “BadRequest_MethodNotAllowed”,
        “message”: “‘PATCH’ requests for ‘ZYSalesOrders’ of EdmType ‘Collection’ are not allowed within Dynamics 365 Business Central OData web services.”
    }
}

Why? The reason is actually very simple. If you use Get Method, you will find that the result is not only one record, but a collection. In BC, it is not supported to modify all records through OData web services.

https://api.businesscentral.dynamics.com/v2.0/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox221/ODataV4/Company('My%20Company')/ZYSalesOrders

But there is a trap here, if you add filtering and only get one record, this is also not supported. More details about $filter.

https://api.businesscentral.dynamics.com/v2.0/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox221/ODataV4/Company('My%20Company')/ZYSalesOrders?$filter=No eq '101001'

At this time we need to specify the primary key in the endpoint.

https://api.businesscentral.dynamics.com/v2.0/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox221/ODataV4/Company('My%20Company')/ZYSalesOrders(Document_Type='Order',No='101001')

If you use a standard page and publish it in the Web Service, you may also encounter the following error. It’s also very simple, this page is set to be uneditable.

{
    “error”: {
        “code”: “BadRequest_MethodNotAllowed”,
        “message”: “Entity does not support modifying data.  CorrelationId:  f6ce68c7-f544-497b-9971-cbd5fb036320.”
    }
}

For example, page 9305 “Sales Order List”

So if you want to use Odata to update Sales Header data, you need to recreate a new page.
Let’s see a simple example.

page 50011 "ZY Sales Orders"
{
    ApplicationArea = All;
    Caption = 'ZY Sales Orders';
    PageType = List;
    SourceTable = "Sales Header";
    UsageCategory = Lists;

    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("Document Type"; Rec."Document Type")
                {
                    ToolTip = 'Specifies the value of the Document Type field.';
                }
                field("No."; Rec."No.")
                {
                    ToolTip = 'Specifies the number of the estimate.';
                }
                field("Sell-to Customer No."; Rec."Sell-to Customer No.")
                {
                    ToolTip = 'Specifies the number of the customer associated with the sales return.';
                }
                field("Posting Date"; Rec."Posting Date")
                {
                    ToolTip = 'Specifies the date when the sales document was posted.';
                }
                field("Salesperson Code"; Rec."Salesperson Code")
                {
                    ToolTip = 'Specifies the name of the salesperson who is assigned to the customer.';
                }
                field("Location Code"; Rec."Location Code")
                {
                    ToolTip = 'Specifies the location from where inventory items to the customer on the sales document are to be shipped by default.';
                }
                field("Payment Terms Code"; Rec."Payment Terms Code")
                {
                    ToolTip = 'Specifies a formula that calculates the payment due date, payment discount date, and payment discount amount.';
                }
            }
        }
    }
}

Publish it to the Web Services.

Patch:

Test video:

PS:
1. If you want to set up an OData connection, for performance and stability reasons consider using an API page instead. API documentation

2. SOAP endpoints (warning): The capability of exposing SOAP endpoints will be removed in a later release. (SOAP has been superseded by OData V4. It’s recommended that integrations are migrated to OData V4 as soon as possible.)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL