Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Programmatically applying a payment to a posted sales invoice

(0) ShareShare
ReportReport
Posted on by 5

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? 

  • Samantha73 Profile Picture
    2,847 on at
    RE: Programmatically applying a payment to a posted sales invoice

    I did find another blog where posted entries are applied:

    microsoft-dynamicsnav.blogspot.com/.../dynamics-nav-2016-applying-and-posting.html

    However, not sure if it's a good practise to set applied-to-id directly from code instead of going through the BC codunit so now my code looks like (currently hard coded values)

            CustomerLdgEntryInv.RESET;//CustomerLdgEntryInv is a record variable of table 21
            CustomerLdgEntryInv.SETRANGE(CustomerLdgEntryInv."Document Type", CustomerLdgEntryInv."Document Type"::Invoice);//Find only Invoices
            CustomerLdgEntryInv.SETRANGE(CustomerLdgEntryInv.Open, TRUE);// Find only Open entries    to apply
            CustomerLdgEntryInv.SetRange("Document No.", 'Inv0000006');
    
            IF CustomerLdgEntryInv.FINDFIRST THEN BEGIN
                // Search for Payment entries in customer ledger entries
                CustomerLdgEntryPay.RESET;//CustomerLdgEntryInv is a record variable of table 21
                CustomerLdgEntryPay.SETRANGE(CustomerLdgEntryPay."Document Type", CustomerLdgEntryPay."Document Type"::Payment);// Find the payment
                CustomerLdgEntryPay.SETRANGE(CustomerLdgEntryPay.Open, TRUE);// Find the payment Ledger Entry
                CustomerLdgEntryPay.SetRange("Document No.", 'GCR0000004');
                IF CustomerLdgEntryPay.FINDFIRST THEN
                    REPEAT//Repeat for all the payment entries
                          //set applies to ID
                        CustomerLdgEntryPay.CALCFIELDS(Amount);
                        //Update all the invoice entries
                        CustomerLdgEntryInv.CALCFIELDS(Amount);
                        CustomerLdgEntryInv.validate("Applying Entry", TRUE);
                        CustomerLdgEntryInv.validate("Applies-to ID", CustomerLdgEntryPay."Document No.");
                        CustomerLdgEntryInv.CALCFIELDS("Remaining Amount");
                        CustomerLdgEntryInv.VALIDATE("Amount to Apply", 100.01);//CustomerLdgEntryInv."Remaining Amount");
                        CODEUNIT.RUN(CODEUNIT::"Cust. Entry-Edit", CustomerLdgEntryInv);
                        COMMIT;// Commit the change
                        SetAppliesToID.SetApplId(CustomerLdgEntryPay, CustomerLdgEntryInv, CustomerLdgEntryPay."Document No.");// SatAppliesToID is a codeUnit variable of Code Unit 101
                                                                                                                               //SalesInvoiceHeader.SetRange("No.", CustomerLdgEntryInv."Document No.");
                                                                                                                               //if SalesInvoiceHeader.FindFirst() then                                                                                                     // Post the application
                        PostAppn.Apply(CustomerLdgEntryInv, CustomerLdgEntryInv."Document No.", CustomerLdgEntryPay."Posting Date"); // PostAppn is a codeUnit variable of CodeUnit 226
                    UNTIL CustomerLdgEntryPay.NEXT = 0;
            END;

  • Samantha73 Profile Picture
    2,847 on at
    RE: Programmatically applying a payment to a posted sales invoice

    In this example , I tried to apply already posted entries but ultimately I want to create the payment in unposted mode (with applied entries) and then post - the same sequence as from the front end when creating a Cash receipt Journal. Ideally create an unposted entry for users to review

  • Inge M. Bruvik Profile Picture
    1,025 Moderator on at
    RE: Programmatically applying a payment to a posted sales invoice

    Are you trying to do it at the same time as you post the payment or are you trying to apply already posted entries against each other?

  • Samantha73 Profile Picture
    2,847 on at
    RE: Programmatically applying a payment to a posted sales invoice

    I'm trying this myself, did you find out the solution

  • Suggested answer
    Marco Mels Profile Picture
    on at
    RE: Programmatically applying a payment to a posted sales invoice

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
YUN ZHU Profile Picture

YUN ZHU 628 Super User 2025 Season 1

#2
Mansi Soni Profile Picture

Mansi Soni 495

#3
Sagar Dangar, MCP Profile Picture

Sagar Dangar, MCP 395

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans