AX 2012 Create and post Vendor Invoice Journals

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.clearField(fieldNum(ledgerJournalTrans, offsetLedgerDimension), false); 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(); } } }
posted @ 2016-12-01 12:05  Fog-Fog  阅读(282)  评论(0编辑  收藏  举报