Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

How to create a GET API using Code unit.

Posted on by 47
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.
Categories:
  • Suggested answer
    Sami Ullah Profile Picture
    Sami Ullah 257 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 195 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans