web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

AX Sales Invoice Settlements

(0) ShareShare
ReportReport
Posted on by 685

hello,

I'am trying to settle Sales invoices while integrating the Payments through x++, i have all the invoices numbers and the amounts to be settled ( it can be partial or full settlement); Below is my code; 

I need your help in fixing the below since the settlement is not working properly;

public void ApplyToInvoices(JournalId _journalNum, str _paymentCode, LedgerAccount _customerAccount )
{
CustVendOpenTransManager manager;
CustTrans custTrans;
CustTransOpen custTransOpen;
LedgerJournalTrans ledgerJournalTrans;
AxLedgerJournalTable axLedgerJournalTable;
AxLedgerJournalTrans axLedgerJournalTrans;
ExchangeRateHelper exchangeRateHelper;
AmountCur totalSettleAmount;
InvoiceId _ledgerJournalId;
ResultSet AppliedInvoices;

try
{

 AxLedgerJournalTable = AxLedgerJournalTable::newLedgerJournalTable(LedgerJournalTable::find(_journalNum));
 ttsBegin;
//details journal lines from Pay Journal Details
 AppliedInvoices = this.getAppliedInvoices(_paymentCode);
while (AppliedInvoices.next()) { _ledgerJournalId = AppliedInvoices.getString(1); totalSettleAmount = AppliedInvoices.getReal(2); CustTrans = CustTrans::findFromInvoice(_ledgerJournalId,_customerAccount); CustTransOpen = CustTransOpen::findRefId(CustTrans.RecId); //Create Trans ... axLedgerJournalTrans = new axLedgerJournalTrans(); axLedgerJournalTrans.parmJournalNum(AxLedgerJournalTable.ledgerJournalTable().JournalNum); axLedgerJournalTrans.parmTransDate(today()); axLedgerJournalTrans.parmDue(today()); axLedgerJournalTrans.parmAccountType(LedgerJournalACType::Cust); axLedgerJournalTrans.parmLedgerDimension(DimensionStorage::getDynamicAccount((_customerAccount), LedgerJournalACType::Cust)); axLedgerJournalTrans.save(); manager = CustVendOpenTransManager::construct(axLedgerJournalTrans.ledgerJournalTrans()); manager.updateTransMarked(custTransOpen,true); manager.updateSettleAmount(custTransOpen,totalSettleAmount); //update after settle ledgerJournalTrans = axLedgerJournalTrans.ledgerJournalTrans(); ledgerJournalTrans.selectForUpdate(true); ledgerJournalTrans.AmountCurDebit = abs(totalSettleAmount); ledgerJournalTrans.SettleVoucher = SettlementType::SelectedTransact; ledgerJournalTrans.MarkedInvoice = _ledgerJournalId; ledgerJournalTrans.update(); } ttsCommit; } catch { info ("Auto Settlement Error in Integration Code"); } }

*This post is locked for comments

I have the same question (0)
  • Vilmos Kintera Profile Picture
    46,149 on at

    Please edit your post, use the Rich formatting, and use the Code tag in order to make your pasted code have proper formatting instead of being a wall of text.

  • Serge Abi Dib Profile Picture
    685 on at
    public void ApplyToInvoices(JournalId _journalNum, str _paymentCode, LedgerAccount _customerAccount )
    {
    CustVendOpenTransManager manager;
    CustTrans custTrans;
    CustTransOpen custTransOpen;
    LedgerJournalTrans ledgerJournalTrans;
    AxLedgerJournalTable axLedgerJournalTable;
    AxLedgerJournalTrans axLedgerJournalTrans;
    ExchangeRateHelper exchangeRateHelper;
    AmountCur totalSettleAmount;
    //AmountCur amountCurDebit,amountCurCredit;
    InvoiceId _ledgerJournalId;
    ResultSet AppliedInvoices;
    //int Row;
    try
    {
    AxLedgerJournalTable = AxLedgerJournalTable::newLedgerJournalTable(LedgerJournalTable::find(_journalNum));
    ttsBegin;
    //details journal lines from Pay Journal Details
    AppliedInvoices = this.getAppliedInvoices(_paymentCode);
    while (AppliedInvoices.next())
    {
    _ledgerJournalId = AppliedInvoices.getString(1);
    totalSettleAmount = AppliedInvoices.getReal(2);
    //CustTrans = CustTrans::findFromInvoice(hrsPayJournalDetails.ReferenceNumber,HRSPayJournalDetails.AccountNum);
    CustTrans = CustTrans::findFromInvoice(_ledgerJournalId,_customerAccount);
    CustTransOpen = CustTransOpen::findRefId(CustTrans.RecId);
    
    //if(CustTransOpen)
    //{
    //Create Trans ...
    axLedgerJournalTrans = new axLedgerJournalTrans();
    axLedgerJournalTrans.parmJournalNum(AxLedgerJournalTable.ledgerJournalTable().JournalNum);
    axLedgerJournalTrans.parmTransDate(today());
    axLedgerJournalTrans.parmDue(today());
    axLedgerJournalTrans.parmAccountType(LedgerJournalACType::Cust);
    axLedgerJournalTrans.parmLedgerDimension(DimensionStorage::getDynamicAccount((_customerAccount), LedgerJournalACType::Cust));
    //axLedgerJournalTrans.parmAmountCurCredit(hrsPayJournalDetails.Credit);
    // axLedgerJournalTrans.parmAmountCurDebit(hrsPayJournalDetails.Debit);
    axLedgerJournalTrans.save();
    manager = CustVendOpenTransManager::construct(axLedgerJournalTrans.ledgerJournalTrans());
    manager.updateTransMarked(custTransOpen,true);
    manager.updateSettleAmount(custTransOpen,totalSettleAmount);
    /*if(hrsPayJournalDetails.Debit)
    {
    manager.updateSettleAmount(custTransOpen,hrsPayJournalDetails.Debit);
    }
    else
    {
    manager.updateSettleAmount(custTransOpen,hrsPayJournalDetails.Credit);
    }
    */
    //update after settle
    ledgerJournalTrans = axLedgerJournalTrans.ledgerJournalTrans();
    ledgerJournalTrans.selectForUpdate(true);
    ledgerJournalTrans.AmountCurDebit = abs(totalSettleAmount);
    ledgerJournalTrans.SettleVoucher = SettlementType::SelectedTransact;
    ledgerJournalTrans.MarkedInvoice = _ledgerJournalId;
    ledgerJournalTrans.update();
    //Row++;
    //}
    }
    ttsCommit;
    //info(strFmt("Total Lines settled : %1",Row));
    }
    catch
    {
    info ("Auto Settlement Error in Integration Code");
    }
    }
  • Vilmos Kintera Profile Picture
    46,149 on at

    Well, we still miss indentation and is hard to read.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at
  • Serge Abi Dib Profile Picture
    685 on at

    Hi Vilmos,

    My code is extracted from the examples you've posted, I need someone to get throw my code and tell me where the issue since when it is running, the payment journal will be updated and lines will be added (the numbers of lines inserted are equal to number of invoices partially or fully settled); i.e if a payments settle 4 invoices, 4 lines will be added in the journal.

    To be noted that the Total debit is not equal total credit of the journal voucher

    in addition if you go through the "Functions-->Settlement", you find that there are errors next to each invoice line mentioning "Invoice is already settled" but in fact it is not.

    attached is a screenshot for an example where a payment of total amount is 736.71 USD and it should settle 4 invoices

    After-settlement.png

  • Verified answer
    Serge Abi Dib Profile Picture
    685 on at

    It is solved, after saving the payment header and lines, i used 'While Loop' and call the below function for each payment line.

    public void ApplyToInvoices(JournalId    _journalNum, str _paymentCode, LedgerAccount _customerAccount, Voucher _voucher)
    {
        CustVendOpenTransManager manager;
        CustTrans                custTrans;
        CustTransOpen            custTransOpen;
        LedgerJournalTrans       ledgerJournalTrans;
    
        AxLedgerJournalTable     axLedgerJournalTable;
        AxLedgerJournalTrans     axLedgerJournalTrans;
        ExchangeRateHelper       exchangeRateHelper;
        AmountCur                totalSettleAmount;
        //AmountCur                amountCurDebit,amountCurCredit;
        InvoiceId         _ledgerJournalId;
        ResultSet               AppliedInvoices;
    
        //int Row;
    
        try
        {
    
        ttsBegin;
        //details journal lines from Pay Journal Details
        AppliedInvoices = this.getAppliedInvoices(_paymentCode);
        while (AppliedInvoices.next())
        {
            _ledgerJournalId = AppliedInvoices.getString(1);
            totalSettleAmount = AppliedInvoices.getReal(2);
    
          
            CustTrans    = CustTrans::findFromInvoice(_ledgerJournalId,_customerAccount);
            CustTransOpen  = CustTransOpen::findRefId(CustTrans.RecId);
    
    
            
                ledgerJournalTrans = LedgerJournalTrans::find(_journalNum,_voucher,NoYes::Yes);
           
                manager = CustVendOpenTransManager::construct(ledgerJournalTrans);
                manager.updateTransMarked(custTransOpen,true);
    
                manager.updateSettleAmount(custTransOpen,totalSettleAmount);
    
                //update after settle
                //ledgerJournalTrans = axLedgerJournalTrans.ledgerJournalTrans();
    
                ledgerJournalTrans.selectForUpdate(true);
                //ledgerJournalTrans.AmountCurDebit = abs(totalSettleAmount);
                ledgerJournalTrans.SettleVoucher =  SettlementType::SelectedTransact;
                ledgerJournalTrans.MarkedInvoice = _ledgerJournalId;
                ledgerJournalTrans.update();
                //Row++;
            //}
    
        }
        ttsCommit;
    
        //info(strFmt("Total Lines settled : %1",Row));
        }
        catch
        {
            info ("Auto Settlement Error in Integration Code");
        }
    
    }
    


  • mohamed amer Profile Picture
    85 on at

    Hi Serge A-D,

    please let me know where you are going to paste this code ??

  • Tony Mok Profile Picture
    577 on at

    CustTrans = CustTrans::findFromInvoice(_ledgerJournalId,_customerAccount);
    CustTransOpen = CustTransOpen::findRefId(CustTrans.RecId);

    It causes the problem when the same invoice is partially settled before. The code is rewritten as follows:

    select CustTransOpen
        join CustTrans
            where CustTransOpen.RefRecId == CustTrans.RecId
               && CustTrans.Invoice == _ledgerJournalId
               && CustTrans.AccountNum == _customerAccount;

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 39

#2
Michel ROY Profile Picture

Michel ROY 14

#3
imran ul haq Profile Picture

imran ul haq 8

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans