Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

In AL extension, when calling custom API via POST, Posting Description isn't as expected.

Posted on by 2
Hello team.
I have created a custom web API in an AL extension with OData. When calling this custom API via a POST method and providing a value for the Posting Description field, after checking in Business Central, the invoice is created, but the value in the Posting Description field is different from the one I passed. Instead, it appears to default to the Invoice Prefix value and Invoice Record Number.

Below, I have shared sample code for creating purchase invoices.

 
page 50083 PInvoiceCreate
{
    ODataKeyFields = /No./;
    SourceTable = /Purch. Inv. Header/;
    PageType = API;
    Caption = 'PInvoice Create';
    APIPublisher = 'MyPublisher';
    APIVersion = 'v1.0';
    APIGroup = 'businessCentral';
    EntityName = 'purchaseInvoice';
    EntitySetName = 'purchaseInvoice';
    DelayedInsert = true;
    Extensible = false;
    layout
    {
        area(Content)
        {
            group(GroupName)
            {
                field(number; Rec./No./)
                {
                    Caption = 'No.';
                    Editable = false;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(Rec.FieldNo(/No./));
                    end;
                }
                field(vendorNo; rec./Buy-from Vendor No./)
                {
                    Caption = 'Buy-from Vendor No.';
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(rec.FieldNo(/Buy-from Vendor No./));
                    end;
                }
                field(buyFromVendorName; rec./Buy-from Vendor Name/)
                {
                    Caption = 'Buy-from Vendor Name';
                    ApplicationArea = All;
                }
                field(dueDate; rec./Due Date/)
                {
                    Caption = 'Due Date';
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(Rec.FieldNo(/Due Date/));
                    end;
                }
                field(vendorInvoiceNumber; rec./Vendor Invoice No./)
                {
                    Caption = 'Vendor Invoice No';
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(rec.FieldNo(/Vendor Invoice No./));
                    end;
                }
                field(postingDate; rec./Posting Date/)
                {
                    Caption = 'Posting Date';
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(rec.FieldNo(/Posting Date/));
                    end;
                }
                field(postingDescription; rec./Posting Description/)
                {
                    Caption = 'Posting Description';
                    ApplicationArea = All;
                    Editable = true;
                    Enabled = true;
                    Visible = true;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(Rec.FieldNo(/Posting Description/));
                    end;
                }
                field(onHold; rec./On Hold/)
                {
                    Caption = 'On Hold';
                    ApplicationArea = All;
                    Editable = true;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(rec.FieldNo(/On Hold/));
                    end;
                }
                field(remitToCode; rec./Remit-to Code/)
                {
                    Caption = 'Remit-to Code';
                    ApplicationArea = All;
                    Editable = true;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(rec.FieldNo(/Remit-to Code/));
                    end;
                }
                field(purchaserCode; rec./Purchaser Code/)
                {
                    Caption = 'Purchaser Code';
                    ApplicationArea = All;
                    Editable = true;
                    trigger OnValidate()
                    begin
                        RegisterFieldSet(rec.FieldNo(/Purchaser Code/));
                    end;
                }
            }
        }
    }
    actions
    {
    }
    local procedure RegisterFieldSet(FieldNo: Integer)
    var
        LastOrderNo: Integer;
        ErrorMsg: Text;
    begin
        LastOrderNo := 1;
        if TempFieldBuffer.FindLast() then
            LastOrderNo := TempFieldBuffer.Order + 1;
        Clear(TempFieldBuffer);
        TempFieldBuffer.Order := LastOrderNo;
        TempFieldBuffer./Table ID/ := Database::/Purch. Inv. Header/;
        TempFieldBuffer./Field ID/ := FieldNo;
        TempFieldBuffer.Insert();
    end;
    local procedure CheckPermissions()
    var
        PurchaseHeader: Record /Purchase Header/;
    begin
        PurchaseHeader.SetRange(/Document Type/, PurchaseHeader./Document Type/::Invoice);
        if not PurchaseHeader.ReadPermission() then
            Error(PurchaseInvoicePermissionsErr);
        HasWritePermission := PurchaseHeader.WritePermission();
    end;
    trigger OnInsertRecord(BelowxRec: Boolean): Boolean
    var
    begin
        PropagateOnInsert(rec, TempFieldBuffer);
        exit(false);
    end;
    procedure PropagateOnInsert(var PurchInv: Record /Purch. Inv. Header/; var TempFieldBuffer: Record /Field Buffer/ temporary)
    var
        PurchaseHeader: Record /Purchase Header/;
        TargetRecordRef: RecordRef;
        DocTypeFieldRef: FieldRef;
        NoFieldRef: FieldRef;
    begin
        TargetRecordRef.Open(DATABASE::/Purchase Header/);
        DocTypeFieldRef := TargetRecordRef.Field(PurchaseHeader.FieldNo(/Document Type/));
        DocTypeFieldRef.Value(PurchaseHeader./Document Type/::Invoice);
        NoFieldRef := TargetRecordRef.Field(PurchaseHeader.FieldNo(/No./));
        TransferFieldsWithValidate(TempFieldBuffer, PurchInv, TargetRecordRef);
        TargetRecordRef.Insert(true);
    end;
    local procedure TransferFieldsWithValidate(var TempFieldBuffer: Record /Field Buffer/ temporary; RecordVariant: Variant; var TargetTableRecRef: RecordRef)
    var
        DataTypeManagement: Codeunit /Data Type Management/;
        SourceRecRef: RecordRef;
        TargetFieldRef: FieldRef;
        SourceFieldRef: FieldRef;
    begin
        DataTypeManagement.GetRecordRef(RecordVariant, SourceRecRef);
        TempFieldBuffer.Reset();
        if not TempFieldBuffer.FindFirst() then
            exit;
        repeat
            if TargetTableRecRef.FieldExist(TempFieldBuffer./Field ID/) then begin
                SourceFieldRef := SourceRecRef.Field(TempFieldBuffer./Field ID/);
                TargetFieldRef := TargetTableRecRef.Field(TempFieldBuffer./Field ID/);
                if TargetFieldRef.Class = FieldClass::Normal then begin
                    TargetFieldRef.Value(SourceFieldRef.Value);
                    TargetFieldRef.Validate(SourceFieldRef.Value);
                end;
            end;
        until TempFieldBuffer.Next() = 0;
    end;
   
    trigger OnOpenPage()
    begin
        CheckPermissions();
    end;
    var
        PurchaseInvoicePermissionsErr: Label 'You do not have permissions to read Purchase Invoices.';
        HasWritePermission: Boolean;
        TempFieldBuffer: Record /Field Buffer/ temporary;
}
Categories:
  • Verified answer
    Mohana Yadav Profile Picture
    Mohana Yadav 59,137 Super User 2024 Season 2 on at
    In AL extension, when calling custom API via POST, Posting Description isn't as expected.
    It must be replacing in TransferFieldsWithValidate function.
    Try to skip the field Posting Description in this function.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans