web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

How to create a GET API using Code unit.

(1) ShareShare
ReportReport
Posted on by 163
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.
I have the same question (0)
  • Suggested answer
    Ramiz Profile Picture
    597 on at
    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/

     
  • Suggested answer
    Sami Ullah Profile Picture
    315 Moderator on at
    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
    YUN ZHU Profile Picture
    95,729 Super User 2025 Season 2 on at
    Hi, hope the following helps as well.
    Dynamics 365 Business Central: How to use Standard/Custom API Bound Actions
     
    Thanks.
    ZHU
  • Suggested answer
    Gerardo Rentería García Profile Picture
    25,268 Most Valuable Professional on at
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,604 Super User 2025 Season 2 on at
    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,135

#2
YUN ZHU Profile Picture

YUN ZHU 733 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 612

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans