最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

Create and post Vendor Invoice Journals X++ Dynamics Ax 2012

AX 2012 alirazazaidi 3997浏览

Create and post Vendor Invoice Journals from code as follow.

static void  createVendorInvoiceJournal(Args _args)
{
    LedgerJournalCheckPost  jourCheckPost;
    LedgerJournalTable      jourTable;

    AxLedgerJournalTable    header  = new AxLedgerJournalTable();
    AxLedgerJournalTrans    trans   = new AxLedgerJournalTrans();

    container               offsetDim;

    // The AP invoice journal name, could vary.
    LedgerJournalNameId     ledgerJournalNameId = "AP";

    // This is the entry against the Vendor account.
    // It will have to pre-exist for this script to work.
    // It is probably created the first time such an entry is
    // made on a journal line.
    DimensionAttributeValueCombination  davc;

    LedgerJournalACType         accType, offsetAccType;

    BankAccountTable            bankAccountTable;

    accType         = LedgerJournalACType::Vend;
    offsetAccType   = LedgerJournalACType::Ledger;

    header.parmJournalName(ledgerJournalNameId);
    header.parmJournalType(LedgerJournalType::VendInvoiceRegister);
    header.save();

    trans.parmAccountType(accType);
    trans.parmJournalNum(header.ledgerJournalTable().JournalNum);

    offsetDim   = ["52121-Disp", "52121", 2, "Site", "OK", "Department", "204"];    //First is Display value, followed by Main Account and then dimensions.

    trans.parmAccountType(LedgerJournalACType::Vend);

    // Note: This only works if the DimensionValueAttributeCombindation record
    // exists from before.
    select firstonly RecId from davc
        where davc.DisplayValue == "Vendor123"; //Vendor123 is the vendor account number.

    trans.parmLedgerDimension(davc.RecId);

    //There's a parm method for credit as well.
    trans.parmAmountCurDebit(99.15);
    trans.parmOffsetAccountType(offsetAccType);
    trans.parmOffsetLedgerDimension(AxdDimensionUtil::getLedgerAccountId(offsetDim));

    trans.save();

    jourTable =  header.ledgerJournalTable();

    if (jourTable.RecId > 0)
    {
        jourCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(jourTable,
                                                                NoYes::Yes,
                                                                NoYes::Yes);
        // Post only if there is succesful validation.
        if (jourCheckPost.validate())
        {
            jourCheckPost.run();
        }
    }
}