Wednesday, February 4, 2015

Create Purchase invoice journal and lines through code X++ in Ax2012.

//Working code and tested in same instance..
static void createVendorInvoiceJournal(Args args)
{
    LedgerJournalCheckPost                  jourCheckPost;
    LedgerJournalTable                          jourTable;
    InventTable                                       inventTable;
    DimensionAttributeValueSet            dimAttrValueSet;
    DimensionAttributeValueSetItem     dimAttrValueSetItem;
    DimensionAttributeValue                 dimAttrValue;
    DimensionAttribute                          dimAttr;
    Common                                           dimensionValueEntity;
    AxLedgerJournalTable header = new AxLedgerJournalTable();
    AxLedgerJournalTrans trans = new AxLedgerJournalTrans();
    AxLedgerJournalTrans trans1 = new AxLedgerJournalTrans();
    container            offsetDim;
    str 30      accNo,departmentDim,businessUnitDim,costCenterDim,itemGroupDim;
    str 30      businessUnitDimValue,costCenterDimValue,itemGroupDimValue,departmentDimValue;
    int           indexcount;
     
    LedgerJournalNameId ledgerJournalNameId = "APInvoice";
    DimensionAttributeValueCombination davc;

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

    trans.parmAccountType(LedgerJournalACType::Vend);
    trans.parmJournalNum(header.ledgerJournalTable().JournalNum);

    select firstonly RecId from davc where davc.DisplayValue == "CN-001";    
    trans.parmLedgerDimension(davc.RecId);
    trans.parmAmountCurCredit(99.15);
    trans.parmTaxItemGroup("UB");
    trans.save();

    trans1.parmOffsetAccountType(LedgerJournalACType::Ledger);
    //dimAttrValueSet = DimensionAttributeValueSet::find(inventTable.DefaultDimension);
    dimAttrValueSet = DimensionAttributeValueSet::find(22565462243);

    while select dimAttrValueSetItem where dimAttrValueSetItem.DimensionAttributeValueSet ==  
     dimAttrValueSet.RecId
    {
        dimAttrValue = DimensionAttributeValue::find 
                                                            (dimAttrValueSetItem.DimensionAttributeValue);
        dimAttr = DimensionAttribute::find(dimAttrValue.DimensionAttribute);
        dimensionValueEntity = DimensionDefaultingControllerBase::findBackingEntityInstance
         (curext(),
        dimAttr, dimAttrValue.EntityInstance);

        if(dimAttr.Name == "Department")
        {
            departmentDim = dimAttr.Name;
            departmentDimValue = dimAttrValue.getValue();
        }
        if(dimAttr.Name == "BusinessUnit")
        {
            businessUnitDim = dimAttr.Name;
            businessUnitDimValue = dimAttrValue.getValue();
        }
        if(dimAttr.Name == "CostCenter")
        {
            costCenterDim = dimAttr.Name;
            costCenterDimValue = dimAttrValue.getValue();
        }
        if(dimAttr.Name == "ItemGroup")
        {
            itemGroupDim = dimAttr.Name;
            itemGroupDimValue = dimAttrValue.getValue();
        }
  }
    accNo = DimensionAttributeValueCombination::getDisplayValue(WNXParameters::find().VendorLedgerDimension);
    indexcount = 2; trans1.parmJournalNum(header.ledgerJournalTable().JournalNum);
    //First is  Display value, followed by Main Account and then dimensions.
    offsetDim =
  [accNo,accNo,indexcount,businessUnitDim,businessUnitDimValue,departmentDim,departmentDimValue];
    //offsetDim = ["110180", "110180", 2, "BusinessUnit", "001", "Department", "022"];
    //Manual input
    //offsetDim = ["112000", "112000", 0]; //Manual input
    trans1.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(offsetDim));
    trans1.parmAmountCurDebit(99.15);
    trans1.parmTaxItemGroup("UB");
    trans1.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();
        }
    }
}

No comments: