Friday, March 27, 2020

Create vendor pending invoices for the invoice pool - using batch job in the Accounts Payable Module.

In D365FO - Creating the vendor pending invoice using batch job:


public void process(VendPendingInvoiceContract _contract)
{
       QueryRun                queryRun;
       LedgerJournalTrans      ledgerTrans;
Query                   query = _contract.getQuery();
boolean                 hasError = false;

        query.dataSourceTable(tableNum(LedgerJournalTrans)).addRange(fieldNum(LedgerJournalTrans, AccountType)).value(queryValue(LedgerJournalACType::Vend));
        query.dataSourceTable(tableNum(LedgerJournalTable)).addRange(fieldNum(LedgerJournalTable, JournalType)).value(queryValue(LedgerJournalType::VendInvoicePool));
        query.dataSourceTable(tableNum(LedgerJournalTable)).addRange(fieldNum(LedgerJournalTable, Posted)).value(queryValue(NoYes::No));
      
        queryRun = new QueryRun(query);
       
        while (queryRun.next())
        {
            try
            {
                ttsbegin;
                ledgerTrans = queryRun.get(tableNum(LedgerJournalTrans));
                if (!ledgerTrans.hasPendingInvoice())
                {
                    this.autoApprovalJournal(ledgerTrans);
                }
                ttscommit;
            }
            catch (Exception::Error)
            {
                if(!hasError)
                {
                    error(strfmt("PendingInvoiceError"));
                    hasError = true;
                }
            }
        }
           
    }

The definition of the auotApprovalJouranl method below:



public void autoApprovalJournal(LedgerJournalTrans _ledgerJournalTrans)
{
    PurchFormLetter             purchFormLetter;
    PurchParmUpdate             purchParmUpdate;
    PurchTable                  purchTableOrig;
       
    purchTableOrig = PurchTable::find(_ledgerJournalTrans.PurchIdRange);
       
    Purchformletter = purchformletter::construct(DocumentStatus::ApproveJournal);
    purchformletter.parmParmTableNum(_ledgerJournalTrans.Invoice);
    purchFormLetter.parmSourceTable(purchTableOrig);
    Purchformletter.parmLedgerJournalTrans(_ledgerJournalTrans);
       
    purchParmUpdate = purchformletter.purchParmUpdate();
    purchParmUpdate.SumNum = _ledgerJournalTrans.Invoice;
    purchformletter.initParameters(purchParmUpdate, Printout::Current);
       
    purchformletter.sumBy(AccountOrder::None);
    purchformletter.showQueryForm(false);
    purchformletter.parmProcessAdditional(true);
    purchformletter.createFromLines(false);

    purchformletter.reArrangeNow(true);
    //purchformletter.reArrange();
    purchformletter.specQty(purchupdate::ALL);
    purchformletter.chooseLines(true);

    VendInvoiceInfoTable::moveFromActiveToSaved(VendInvoiceInfoTable::findRecId(purchformletter.parmLastParmTableRecId()));
}

I hope, you will find in some other blogs - how to create vendor pending invoice for the DocumentStatus::Invoice. I have tried but it is not working for the DocumentStatus::ApprovalJouranl. 

Both process will create pending invoice but, there code is different because they are created from different sources.