I am attempting to programmatically apply a payment to a posted sales invoice using the following code:
lSaleInvHeader: record "Sales Invoice Header";
CustomerLedgerEntry: record "Cust. Ledger Entry"; //Payment Ledger Entry
InvCustomerLedgerEntry: record "Cust. Ledger Entry"; //Posted Invoice Ledger Entry
CustEntrySetApplyID: Codeunit "Cust. Entry-SetAppl.ID"; //Codeunit to set the application id
CustEntryApplyPosted: Codeunit "CustEntry-Apply Posted Entries"; //Codeunit to apply the posted entries
begin
CustEntrySetApplyID.SetApplId(InvCustomerLedgerEntry, CustomerLedgerEntry, CustomerLedgerEntry."Document No.");
CustEntryApplyPosted.Apply(InvCustomerLedgerEntry, CustomerLedgerEntry."Document No.", InvCustomerLedgerEntry."Posting Date");
end;
The strange thing is, this code has actually worked once or twice. But now I get the following error when attempting: Cannot post because you did not specify which entry to apply. You must specify an entry in the Applies-to ID field for one or more open entries.
Any suggestions on how I can get this customization working consistently or why I might be facing this error?