Here is the sample code to create project expense journals in D365 FO using X++. In my case, I will retrieve data from a staging table sourced from a third-party system. The job outlined below will use values from the staging table to create the expense journals.
Public final class CreateExpenseJournalService { DaxExpenseStaging StagingTrans; str empId; JournalId journalId; /// <summary> /// This method will create the ledger dimension for expense journals /// </summary> /// <param name = "_conData">DmensionData</param> /// <param name = "_mainAccount">MainAccountNum</param> /// <returns>DimensionDynamicAccount</returns> public DimensionDynamicAccount generateLedgerDimension(container _conData, MainAccountNum _mainAccount) { int hierarchyCount; int hierarchyIdx; RecId dimAttId_MainAccount; LedgerRecId ledgerRecId; MainAccount mainAccount; RefRecId recordvalue; DimensionAttribute dimensionAttribute; DimensionAttributeValue dimensionAttributeValue; DimensionSetSegmentName DimensionSet; DimensionStorage dimStorage; DimensionAttributeValueContract ValueContract; DimensionAttributeValueCombination dimensionAttributeValueCombination; LedgerAccountContract LedgerAccountContract = new LedgerAccountContract(); List valueContracts = new List(Types::Class); try { mainAccount = MainAccount::findByMainAccountId(_mainAccount); recordvalue = DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current()); hierarchyCount = DimensionHierarchy::getLevelCount(recordvalue); DimensionSet = DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue); for(hierarchyIdx = 1;hierarchyIdx<=hierarchyCount;hierarchyIdx++) { if(hierarchyIdx == 1) continue; dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx],false,"en-us"); if(dimensionAttribute) { dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,conPeek(_conData,hierarchyIdx) ,false ,false); if(dimensionAttributeValue) { ValueContract = new DimensionAttributeValueContract(); ValueContract.parmName(dimensionAttribute.Name) ; ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue); valueContracts.addEnd(ValueContract); } } } LedgerAccountContract.parmMainAccount(_mainAccount); LedgerAccountContract.parmValues(valueContracts); dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract); dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save()); ledgerRecId = dimensionAttributeValueCombination.RecId; } catch { throw Exception::error; } return ledgerRecId; } /// <summary> /// A construct method to create object of this class /// </summary> /// <param name = "_stagingTrans">DaxExpenseStaging</param> /// <returns>CreateExpenseJournalService</returns> public static CreateExpenseJournalService construct(DaxExpenseStaging _stagingTrans) { CreateExpenseJournalService createExpenseExpense = new CreateExpenseJournalService(); createExpenseExpense.parmstagingTrans(_stagingTrans); return createExpenseExpense; } /// <summary> /// A parm method to return the current staging /// </summary> /// <param name = "_stagingTrans">DaxExpenseStaging</param> /// <returns>DaxExpenseStaging</returns> public DaxExpenseStaging parmstagingTrans(DaxExpenseStaging _stagingTrans) { StagingTrans = _stagingTrans; return StagingTrans; } /// <summary> /// Method to create the Journal Header /// </summary> /// <returns></returns> public LedgerJournalTable createJournalHeader() { LedgerJournalTable ledgerJournalTable,jourTable1; ProjParameters projParmeters = ProjParameters::find(); ledgerJournalTable.initValue(); ledgerJournalTable.JournalName = LedgerJournalName::find(projParmeters.DAXProjExpenseJournal).JournalName; ledgerJournalTable.initFromLedgerJournalName(); ledgerJournalTable.Name = ledgerJournalTable.ledgerJournalName().Name; ledgerJournalTable.JournalType = ledgerJournalType::Cost; ledgerJournalTable.LedgerJournalInclTax = NoYes::No; ledgerJournalTable.insert(); return ledgerJournalTable; } /// <summary> /// Method used to create the Journal Lines /// </summary> /// <param name = "_ledgerJournalTable">LedgerJournalTable</param> public void createJournalTrans(LedgerJournalTable _ledgerJournalTable) { LedgerJournalTrans ledgerJournalTrans; LedgerJournalTrans_Project ledgerJournalTrans_Project; ProjJournalCheckPost jourPost; ledgerJournalCheckPost ledgerJournalCheckPost; ProjTable projTable; NumberSequenceTable numberSequenceTable; Voucher voucher; ResourceView resourceView; MainAccount mainaccount; ProjParameters projParmeters = ProjParameters::find(); if (_ledgerJournalTable) { select firstonly projTable where projTable.DaxTransID == StagingTrans.TransID; ledgerJournalTrans.clear(); ledgerJournalTrans.JournalNum = _ledgerJournalTable.JournalNum; ledgerJournalTrans.DefaultDimension = projTable.DefaultDimension; ledgerJournalTrans.AccountType = LedgerJournalACType::Project; numberSequenceTable = NumberSequenceTable::find(LedgerJournalName::find(LedgerJournalName::find(projParmeters.DAXProjExpenseJournal).JournalName).NumberSequenceTable); voucher = NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence).voucher(); ledgerJournalTrans.Voucher = voucher; ledgerJournalTrans.CurrencyCode = StagingTrans.Currency; ledgerJournalTrans.Qty = 1; ledgerJournalTrans.TransDate = StagingTrans.JournalDate; ledgerJournalTrans.LedgerDimension = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(projTable.ProjId,LedgerJournalACType::Project); ledgerJournalTrans.modifiedField(fieldNum(ledgerJournalTrans ,LedgerDimension )); ledgerJournalTrans.OffsetAccountType = LedgerJournalACType::Ledger; ledgerJournalTrans.ExchRate = 100; Amount amount = StagingTrans.Costamount; if(amount> 0) { ledgerJournalTrans.AmountCurDebit = amount; } else { ledgerJournalTrans.AmountCurCredit = abs(amount); } ledgerJournalTrans.Company = strUpr(curExt()); ledgerJournalTrans.OffsetCompany = strUpr(curExt()); ledgerJournalTrans.DAXstagingTransactionID = StagingTrans.TransID; // mainaccount = MainAccount::find(projParmeters.DAXReimbursableEmpExpensePayAccount); str mainaccountId = LedgerDimensionFacade::getMainAccountFromLedgerDimension(projParmeters.DAXReimbursableEmpExpensePayAccount).MainAccountId; //MainAccount~CostCenter~ProfitCenter~Product_Family~UniqueProductID~Customer~Vendor container convalues = [mainaccountId, StagingTrans.Costcenter, projParmeters.DAXProfitCenter,'','','','']; refrecid ledgerdim = this.generateLedgerDimension(convalues, mainaccountId); ledgerJournalTrans.OffsetLedgerDimension = LedgerDimensionFacade::serviceCreateLedgerDimension(ledgerdim,projTable.DefaultDimension); ledgerJournalTrans.modifiedField(fieldNum(ledgerJournalTrans ,OffsetLedgerDimension )); ledgerJournalTrans.insert(); if (ledgerJournalTrans.RecId) { ledgerJournalTrans_Project.clear(); ledgerJournalTrans_Project.RefRecId = ledgerJournalTrans.RecId; ledgerJournalTrans_Project.initValue(); ledgerJournalTrans_Project.CategoryId = StagingTrans.Expensecategory; ledgerJournalTrans_Project.ProjId = projTable.ProjId; ledgerJournalTrans_Project.ProjTransDate = StagingTrans.JournalDate; ledgerJournalTrans_Project.modifiedField(fieldNum(ledgerJournalTrans_Project, CategoryId)); ledgerJournalTrans_Project.Qty = 1; ledgerJournalTrans_Project.SalesCurrencyId = ledgerJournalTrans.CurrencyCode; ledgerJournalTrans_Project.CostPrice = amount; //select firstonly resourceView // where resourceView.ResourceId == StagingTrans.EmployeeID; //ledgerJournalTrans_Project.Resource = resourceView.RecId; // If resource need to be filled uncomment the lines ledgerJournalTrans_Project.modifiedField(fieldNum(ledgerJournalTrans_Project, CostPrice)); ledgerJournalTrans_Project.LinePropertyId = ProjLinePropertySetup::findLinePropertyId( projTable.ProjId,ledgerJournalTrans_Project.CategoryId ); boolean priceFound; CostPrice costPriceMST = ProjCostSalesPrice::costPrice(ledgerJournalTrans,ledgerJournalTrans_Project, false); [ledgerJournalTrans_Project.SalesPrice, priceFound] = ProjCostSalesPrice::findCostSalesPrice(ledgerJournalTrans_Project.ProjId,ledgerJournalTrans_Project.Resource,ledgerJournalTrans_Project.CategoryId,costPriceMST,CompanyInfoHelper::standardCurrency(),ledgerJournalTrans_Project.SalesCurrencyId,ledgerJournalTrans_Project.ProjTransDate,ledgerJournalTrans_Project.ProjPriceGroupID,false); ledgerJournalTrans_Project.insert(); } } } }
No comments:
Post a Comment