Skip to main content

Notifications

Announcements

No record found.

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 2
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];
}
  • Aaron Bauldree Profile Picture
    Aaron Bauldree 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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,703 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans