Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Programmatically applying a payment to a posted sales invoice

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? 

Categories:
  • Samantha73 Profile Picture
    Samantha73 2,280 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
    Samantha73 2,280 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
    Inge M. Bruvik 32,744 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
    Samantha73 2,280 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
    Marco Mels 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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,572 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,706 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans