How to set Amount to Apply in Payment Allocation to invoice from code

samantha73samantha73 Member Posts: 96
Hi Guys
I thought it's simple to allocate payments to invoices from code but after couple of days of trying I need help..:)
Basically it works setting the Applied-To-Id and remaining amount , but always takes the remaining amount instead of the amount to apply
This is my simple codeunit
        CustLedgEntry: Record "Cust. Ledger Entry";
        ApplyingCustLedgEntry: Record "Cust. Ledger Entry";
        CustEntrySetApplID: Codeunit "Cust. Entry-SetAppl.ID";
        CustEntryApplyPostedEntries: Codeunit "CustEntry-Apply Posted Entries";


        CustLedgEntry.SetRange("Entry No.", 144);
        ApplyingCustLedgEntry.SetRange("Entry No.", 157);
        CustLedgEntry.Validate("Amount to Apply", -25);

        ApplyingCustLedgEntry.Validate("Applies-to ID", 'TEST1234');
        ApplyingCustLedgEntry.VALIDATE("Amount to Apply", 25);
        CustEntrySetApplID.SetApplId(CustLedgEntry, ApplyingCustLedgEntry, 'test1234');

        ApplyingCustLedgEntry.VALIDATE("Amount to Apply", 25);


        CustEntryApplyPostedEntries.Apply(ApplyingCustLedgEntry, ApplyingCustLedgEntry."Document No.", CustLedgEntry."Posting Date");


so far the only way to get amount to apply populated was changing the setappl.ID codeunit (creating my own) as below
if ((CustLedgerEntry."Amount to Apply" <> 0) and (CustEntryApplID = '')) or
           (CustEntryApplID = '')
            CustLedgerEntry."Amount to Apply" := [b]ApplyingCustLedgerEntry."Amount to Apply" * -1[/b]//-1//original 0
            if CustLedgerEntry."Amount to Apply" = 0 then begin
                CustLedgerEntry.CalcFields("Remaining Amount");
                CustLedgerEntry."Amount to Apply" := [b]ApplyingCustLedgerEntry."Amount to Apply" * -1[/b];//original CustLedgerEntry."Remaining Amount"

I still cannot use above since I need discounts, tolerance etc to be working and want to get see the best possible option. Looks like writing to ledger entries is quite complicated


  • txerifftxeriff Member Posts: 492

    So you want to override "amount to apply" field.
    Why dont you create an event subscriber to onaftervalidate for that field?
    not sure if I understand what do you want to do.
  • samantha73samantha73 Member Posts: 96
    Ok thanks..let me try to explain - it's simple . From the front end you can part pay (invoice remaining 100 payment 50) an invoice though either cash receipt journal or payment journal. How to do this from code?
  • txerifftxeriff Member Posts: 492
    you can use also "applies to do type" and "applies to doc no" fields instead "applies to id". that should solve the issue.

    if you would need to split a payment between several documents proportionally, then you would need to set applies to ID and amount to apply in customer ledger entry/vendor ledger entry.

    I think it's codeunits 226/227 that will allow you to edit both tables.

    just follow the code in nav standard, I've done this a lot years ago but I dont recall precisely.

    hoe this helps.
Sign In or Register to comment.