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.
{
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;
{
area(Content)
{
group(GroupName)
{
field(number; Rec./No./)
{
Caption = 'No.';
Editable = false;
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;
}
{
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;
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;
}
}
}
}
{
}
var
LastOrderNo: Integer;
ErrorMsg: Text;
begin
LastOrderNo := 1;
if TempFieldBuffer.FindLast() then
LastOrderNo := TempFieldBuffer.Order + 1;
TempFieldBuffer.Order := LastOrderNo;
TempFieldBuffer./Table ID/ := Database::/Purch. Inv. Header/;
TempFieldBuffer./Field ID/ := FieldNo;
TempFieldBuffer.Insert();
end;
var
PurchaseHeader: Record /Purchase Header/;
begin
PurchaseHeader.SetRange(/Document Type/, PurchaseHeader./Document Type/::Invoice);
if not PurchaseHeader.ReadPermission() then
Error(PurchaseInvoicePermissionsErr);
end;
var
begin
PropagateOnInsert(rec, TempFieldBuffer);
exit(false);
end;
var
PurchaseHeader: Record /Purchase Header/;
TargetRecordRef: RecordRef;
DocTypeFieldRef: FieldRef;
NoFieldRef: FieldRef;
begin
TargetRecordRef.Open(DATABASE::/Purchase Header/);
DocTypeFieldRef.Value(PurchaseHeader./Document Type/::Invoice);
TargetRecordRef.Insert(true);
end;
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;
begin
CheckPermissions();
end;
PurchaseInvoicePermissionsErr: Label 'You do not have permissions to read Purchase Invoices.';
HasWritePermission: Boolean;
TempFieldBuffer: Record /Field Buffer/ temporary;
}