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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Apply Purchase Order Prepayment during invoice posting through X++ code

(0) ShareShare
ReportReport
Posted on by 758

Hi Everyone,

Need urgent help on below issue,

I am trying to apply prepayment on Purchase Order during invoice posting.

Below code I have found on one of the blogs.

However it is not working in my case. Didn't get any error but the prepayment amount to be applied has not been applied when I run the code.

Please suggest some solution. Thank you in advance.

Link which I referred is,

http://axtechsolutions.blogspot.com/2018/11/job-to-apply-vendor-prepayment-on-po.html

public static void applyPOInvPrepayment(VendInvoiceInfoTable _vendInvoiceInfoTable, Amount   _amountToApply)
    {
        VendAdvanceInvoice              vendAdvanceInvoice;
        VendInvoiceJour                 localVendInvoiceJour;
        tmpVendStandardInvoice          tmpVendStandardInvoice;
        tmpVendAdvanceInvoice           tmpVendAdvanceInvoice;
        PurchTotals                     localPurchTotals;
        VendTrans                       vendTrans, vendTransOffset;
        PurchPrepayTable                purchPrepayTable;
        VendInvoiceInfoSubTable         vendInvoiceInfoSubTable;
        VendInvoiceInfoTable            vendInvoiceInfoTable;

        select firstonly forupdate * from vendInvoiceInfoTable
            where vendInvoiceInfoTable.RecId == _vendInvoiceInfoTable.RecId;

        try
        {
            ttsbegin;
            vendAdvanceInvoice  =   VendAdvanceInvoice::construct(true);
            vendAdvanceInvoice.parmCallerTable(vendInvoiceInfoTable);
            vendAdvanceInvoice.parmIsInvoicePosted(false);
            vendAdvanceInvoice.init();

            if(vendInvoiceInfoTable.InvoiceType == PurchInvoiceType::Standard)
            {
                localPurchTotals = PurchTotals::newParmTable(vendInvoiceInfoTable, PurchUpdate::ReceiveNow,vendInvoiceInfoTable.ParmId, '',DocumentStatus::Invoice);
                localPurchTotals.calc(true);
                
                tmpVendStandardInvoice.clear();

                while select vendInvoiceInfoSubTable
                    where vendInvoiceInfoSubTable.ParmId        == vendInvoiceInfoTable.ParmId
                        && vendInvoiceInfoSubTable.TableRefId   == vendInvoiceInfoTable.TableRefId
                        && vendInvoiceInfoSubTable.OrigPurchId  == vendInvoiceInfoTable.PurchId
                {
                    tmpVendStandardInvoice.clear();
                    tmpVendStandardInvoice.PurchId          =   vendInvoiceInfoSubTable.OrigPurchId;
                    tmpVendStandardInvoice.InvoiceNum       =   vendInvoiceInfoTable.Num;
                    tmpVendStandardInvoice.CurrencyCode     =   vendInvoiceInfoTable.CurrencyCode;
                    tmpVendStandardInvoice.InvoiceDate      =   vendInvoiceInfoTable.TransDate;
                    tmpVendStandardInvoice.InvoiceAccount   =   vendInvoiceInfoTable.InvoiceAccount;
                    tmpVendStandardInvoice.InvoiceAmount    =   localPurchTotals.purchTotalAmount();
                    tmpVendStandardInvoice.insert();
                }
            }

            while select localVendInvoiceJour
                where localVendInvoiceJour.InvoiceType      == PurchInvoiceType::VendorAdvance
                &&     localVendInvoiceJour.InvoiceAccount  == tmpVendStandardInvoice.InvoiceAccount
                &&     localVendInvoiceJour.PurchId         ==  tmpVendStandardInvoice.PurchId
                &&     localVendInvoiceJour.CurrencyCode    ==  tmpVendStandardInvoice.CurrencyCode
                join vendTrans
                where vendTrans.Voucher     == localVendInvoiceJour.LedgerVoucher
                &&    vendTrans.AccountNum  ==  localVendInvoiceJour.InvoiceAccount
                &&    vendTrans.TransDate   ==  localVendInvoiceJour.InvoiceDate
                join purchPrepayTable
                where purchPrepayTable.PurchTable == localVendInvoiceJour.PurchId
                &&  purchPrepayTable.PrepayApplicationRemaining > 0
                exists join vendTransOffset
                where vendTransOffset.RecId     == vendTrans.OffsetRecid
                && vendTransOffset.TransType    == LedgerTransType::Payment
            {
                if(abs(vendTrans.AmountCur) == abs(vendTrans.SettleAmountCur))
                {
                    tmpVendAdvanceInvoice.clear();
                    tmpVendAdvanceInvoice.PurchId         = localVendInvoiceJour.PurchId;
                    tmpVendAdvanceInvoice.InvoiceNum      = localVendInvoiceJour.InvoiceId;
                    tmpVendAdvanceInvoice.CurrencyCode    = localVendInvoiceJour.CurrencyCode;
                    tmpVendAdvanceInvoice.InvoiceDate     = localVendInvoiceJour.InvoiceDate;
                    tmpVendAdvanceInvoice.LedgerVoucher   = localVendInvoiceJour.LedgerVoucher;
                    tmpVendAdvanceInvoice.AdvanceAmount   = localVendInvoiceJour.InvoiceAmount;
                    tmpVendAdvanceInvoice.InvoiceRecId    = localVendInvoiceJour.RecId;
                    tmpVendAdvanceInvoice.AdvanceRemaining = abs(vendTrans.SettleAmountCur);
                    if(tmpVendAdvanceInvoice.AdvanceRemaining > 0)
                    {
                        tmpVendAdvanceInvoice.AppliedToPendingAmount = abs(VendAdvanceInvoice_Application::totalUnpostedAdvanceAppliedAmountForPrepayInvoice(localVendInvoiceJour.RecId));
                        tmpVendAdvanceInvoice.AdvanceRemaining       = tmpVendAdvanceInvoice.AppliedToPendingAmount;
                        tmpVendAdvanceInvoice.insert();
                    }
                }
            }
        
            vendAdvanceInvoice.updateTransMarked(tmpVendStandardInvoice.InvoiceRecId, tmpVendAdvanceInvoice.InvoiceRecId, NoYes::Yes, tmpVendStandardInvoice.SettlementPostingDate);
            vendAdvanceInvoice.updateAdvanceApplicationAmount(tmpVendStandardInvoice.InvoiceRecId, tmpVendAdvanceInvoice.InvoiceRecId, -(_amountToApply));
            vendInvoiceInfoTable.updateExpectedValuesAndMatchStatus();
            ttscommit;
        }
        catch
        {
            ttsabort;
        }

    }

I have the same question (0)
  • Rhushikesh R Profile Picture
    758 on at

    Hi Everyone, please suggest some solution for above query if someone has any idea about it.

  • Suggested answer
    Satish Panwar Profile Picture
    14,671 Moderator on at

    Hi Rhusikesh,

    There are few action items that you can take on to proceed:

    1. Debug above code and see where it's failing so you can identify what's going on. If it's not working at all, then look at below.

    2. From D365 perspective, the standard code is present in class "VendAdvanceInvoice_Application", take a look at that class and debug through it as you perform the process in AX so you know how exactly is this working currently.

    This would give you an idea of how it's working and then you can extract / write your custom code as needed or leverage the standard code to the extent possible. As you will be able to see the D365 payment application, you can also modify the above code and get that to work.

  • Rhushikesh R Profile Picture
    758 on at

    Okay Satish. I will try to debug my code.

    Can you help me on one more query which I have posted on below link,

    community.dynamics.com/.../applying-gst-tax-adjustment-to-vendor-invoice-journal-through-x-code

  • Satish Panwar Profile Picture
    14,671 Moderator on at

    I will take a look but do know i am not familiar with gst-tax-adjustments overall. If above response helped, please take time to help verify the response.

  • AshokKumarGuptaEga Profile Picture
    on at

    Hi Rhusikesh,

    Was the solution shared by Satish helped to resolve the issue?  If so kindly mark this thread as answered or do let us know if need more information.

    Best Regards

    Ashok Kumar Gupta Ega

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 467 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 420 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans