Skip to main content

Notifications

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 151
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
    17,404 Super User 2025 Season 1 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
    Gerardo Rentería García Profile Picture
    19,937 Most Valuable Professional on at
  • Suggested answer
    YUN ZHU Profile Picture
    84,670 Super User 2025 Season 1 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
    315 Super User 2025 Season 1 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
    589 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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 1,452

#2
YUN ZHU Profile Picture

YUN ZHU 1,313 Super User 2025 Season 1

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 1,083 Most Valuable Professional

Featured topics

Product updates

Dynamics 365 release plans