Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

How to create a GET API using Code unit.

Posted on by 97
Hi All,

I am using On-Prem Business Central.

I have created the below code unit to get the sales price, and i have created web service of this code unit also.
now i am trying to call it from POST MAN with post type post, but i am getting error.

Does anyone have any idea about this. Please let me know. 
 
codeunit 50102 "GetItemPrice"
{
    Permissions = tabledata "Price List Line" = rimd;
 
    [ServiceEnabled]
    procedure GetPrice(ItemNo: Text[50]; CustomerNo: Text[50]): Decimal
    var
        PriceListLine: Record "Price List Line";
    begin
        // Set a filter on "Item No." using the provided ItemCode (AssetNo in your case)
        PriceListLine.SetRange("Asset No.", ItemNo);
        PriceListLine.SetRange("Source No.", CustomerNo);
 
        // Check if there is a matching Price List Line and return the Price List Code
        if PriceListLine.FindFirst() then
            exit(PriceListLine."Unit Price")
        else
            exit(0); // Return empty if no matching record is found
    end;
}

Error :  400
 
{
    "error": {
        "code": "BadRequest",
        "message": "Exception of type 'Microsoft.Dynamics.Nav.Service.OData.NavODataBadRequestException' was thrown.  CorrelationId:  6a4a5157-5d90-4ab4-8ebd-fa4713f63e1f."
    }
}

Payload : 
{
    "ItemNo": "111",
    "CustomerNo": "122"
}
Header included all the If Match, Content Type and company id also. but same error.

Thanks for your Help.
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 3,701 on at
    How to create a GET API using Code unit.
    Ensure that your request URL is correctly formatted and that you’re using the right HTTP method. You might also want to double-check the payload structure and confirm that the web service is published properly.
  • Suggested answer
    gdrenteria Profile Picture
    gdrenteria 11,042 Most Valuable Professional on at
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 71,656 Super User 2024 Season 2 on at
    How to create a GET API using Code unit.
    Hi, hope the following helps as well.
    Dynamics 365 Business Central: How to use Standard/Custom API Bound Actions
     
    Thanks.
    ZHU
  • Suggested answer
    Sami Ullah Profile Picture
    Sami Ullah 284 on at
    How to create a GET API using Code unit.
    Hi,

    Example Codeunit:
    codeunit 50100 "SalesOrderProcessing"
    {
        Permissions = tabledata "Sales Header" = rimd;
        
        procedure UpdateSalesOrderStatus(orderno: Text[50]; status: Text[50]): Text
        var
            SalesOrder: Record "Sales Header";
        begin
            SalesOrder.Reset();
            SalesOrder.SetRange("Document Type", "Sales Document Type"::Order);
            SalesOrder.SetRange("No.", orderno);
            if SalesOrder.FindFirst() then begin
                case status of
                    'Open':
                        SalesOrder.Status := SalesOrder.Status::Open;
                    'Released':
                        SalesOrder.Status := SalesOrder.Status::Released;
                end;
                SalesOrder.Modify(true);
            end else
                exit('Sales Order not found.');
        end;
    }



    Example HTTP API Call:
    POST /v2.0/{your tenantID}/Production/ODataV4/SalesOrderProcessing_UpdateSalesOrderStatus
    Host: api.businesscentral.dynamics.com
    Content-Type: application/json
    If-Match: *
    company: 2006d1d5-1a4e-ef11-bfe7-6045bde9c989  // your company ID
    Authorization: Bearer {Provide Token}
    
    {
        "orderno": "S-ORD101001",
        "status": "Released"
    }
  • Suggested answer
    Ramiz Profile Picture
    Ramiz 281 on at
    How to create a GET API using Code unit.
    Hi,

    I created the same and its working fine. Kindly check the url.

    https://api.businesscentral.dynamics.com/v2.0/TenantID/EnvironmentName/ODataV4/MyCode_GetPrice?company={companyId or Name}

    https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-creating-and-interacting-with-odatav4-unbound-action

    https://www.kauffmann.nl/2020/03/05/codeunit-apis-in-business-central/

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans