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

Community site session details

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

Creating a custom API for purchase invoices using AL Extension with OData

(0) ShareShare
ReportReport
Posted on by 8
Hello team,
I have created a custom API for purchase invoices using AL Extension with OData. When I call this API via the GET method, it retrieves data successfully. However, when I call it via the POST method, the response indicates success, but it does not create the invoice in Business Central. Can you please suggest what the mistake in the AL extension code might be?

GET url: /ODataV4/Company({companyName})/CreatePurchaseInvoiceAPI
Post utl: /ODataV4/Company({companyName})/CreatePurchaseInvoiceAPI

Below, I have shared my code for creating purchase invoices.
page 50073 /Custom Purchase Invoice API/
{
    PageType = API;
    Caption = 'Custom Purchase Invoice';
    APIPublisher = 'MyPublisher';
    APIGroup = 'businessCentral';
    APIVersion = 'v1.0';
    EntityName = 'customPurchaseInvoice';
    EntitySetName = 'customPurchaseInvoice';
    DelayedInsert = true;
    SourceTable = /Purch. Inv. Header/;
    SourceTableTemporary = true;
    InsertAllowed = true;
    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field(number; rec./No./)
                {
                    Caption = 'Number';
                }
                field(preAssignedNo; rec./Invoice Disc. Code/)
                {
                    Caption = 'Pre Assigned No';
                }
                field(paymentReference; rec./Payment Reference/)
                {
                    Caption = 'Payment Reference';
                }
                field(buyFromVendorName; rec./Buy-from Vendor Name/)
                {
                    Caption = 'Buy-from Vendor Name';
                }
                field(payToContact; rec./Pay-to Contact/)
                {
                    Caption = 'Pay-to Contact';
                }
                field(orderDate; rec./Order Date/)
                {
                    Caption = 'Order Date';
                }
                field(dueDate; rec./Due Date/)
                {
                    Caption = 'Due Date';
                }
                field(amountIncludingVat; rec./Pay-to Name/)
                {
                    Caption = 'Amount Including VAT';
                }
                field(vendorInvoiceNumber; rec./Vendor Invoice No./)
                {
                    Caption = 'Vendor Invoice No';
                }
                field(postingDescription; rec./Posting Description/)
                {
                    Caption = 'Posting Description';
                }
            }
        }
    }
    trigger OnOpenPage()
    var
        PurchaseInvoice: Record /Purchase Header/;
        PostedPurchaseInvoice: Record /Purch. Inv. Header/;
    begin
        Message('OnOpenPage calling...');
        Clear(DocumentNumberCde);
        DocumentNumberCde := CopyStr(rec.GetFilter(/No./), 1, StrLen(rec.GetFilter(/No./)));
        rec.Init();
        if (DocumentNumberCde <> '') then begin
            //Purchase Invoice
            PurchaseInvoice.Reset();
            PurchaseInvoice.SetRange(/Document Type/, PurchaseInvoice./Document Type/::Invoice);
            PurchaseInvoice.SetRange(/No./, DocumentNumberCde);
            if PurchaseInvoice.FindFirst() then begin
                rec./No./ := PurchaseInvoice./No./;
                rec./Invoice Disc. Code/ := PurchaseInvoice./No./;
                rec./Payment Reference/ := PurchaseInvoice./Payment Reference/;
                rec./Buy-from Vendor Name/ := PurchaseInvoice./Buy-from Vendor Name/;
                rec./Pay-to Contact/ := PurchaseInvoice./Pay-to Contact/;
                rec./Order Date/ := PurchaseInvoice./Order Date/;
                rec./Due Date/ := PurchaseInvoice./Due Date/;
                PurchaseInvoice.CalcFields(/Amount Including VAT/);
                rec./Pay-to Name/ := Format(PurchaseInvoice./Amount Including VAT/);
                rec./Vendor Invoice No./ := PurchaseInvoice./Vendor Invoice No./;
                rec./Posting Description/ := PurchaseInvoice./Posting Description/;
                rec.Insert(true);
                //PurchaseInvoice.Insert(true);
                /*if PurchaseInvoice.Insert(true) then
                    Message(rec./No./ + 'inserted')
                else
                    Message(rec./No./ + 'not inserted');
                */
            end;
        end else begin
            //Purchase Invoice
            PurchaseInvoice.Reset();
            PurchaseInvoice.SetRange(/Document Type/, PurchaseInvoice./Document Type/::Invoice);
            if PurchaseInvoice.FindSet() then
                repeat
                    rec./No./ := PurchaseInvoice./No./;
                    rec./Invoice Disc. Code/ := PurchaseInvoice./No./;
                    rec./Payment Reference/ := PurchaseInvoice./Payment Reference/;
                    rec./Buy-from Vendor Name/ := PurchaseInvoice./Buy-from Vendor Name/;
                    rec./Pay-to Contact/ := PurchaseInvoice./Pay-to Contact/;
                    rec./Order Date/ := PurchaseInvoice./Order Date/;
                    rec./Due Date/ := PurchaseInvoice./Due Date/;
                    PurchaseInvoice.CalcFields(/Amount Including VAT/);
                    rec./Pay-to Name/ := Format(PurchaseInvoice./Amount Including VAT/);
                    rec./Vendor Invoice No./ := PurchaseInvoice./Vendor Invoice No./;
                    rec./Posting Description/ := PurchaseInvoice./Posting Description/;
                    rec.Insert(true);
                    //PurchaseInvoice.Insert(true);
                    /*if PurchaseInvoice.Insert(true) then
                        Message(rec./No./ + 'inserted')
                    else
                        Message(rec./No./ + 'not inserted');
                    */
                until PurchaseInvoice.Next() = 0;
        end;
        rec.Reset();
    end;
    var
        DocumentNumberCde: Code[20];
}
I have the same question (0)
  • Aaron Bauldree Profile Picture
    19 on at
    Creating a custom API for purchase invoices using AL Extension with OData
    Could you send an example of your post request? I have successfully been able to handle a lot of functions similar to your request using API. Also, what version of BC are you currently on as there were some changes that I had to make in my post requests somewhere around v23 (cloud).

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,330

#2
Sumit Singh Profile Picture

Sumit Singh 2,738

#3
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 2,546

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans