Monday, December 30, 2013

Creating a general journal Posting a general journal



Journals in Dynamics AX are manual worksheets that can be posted into the system.One of
the frequently used journals for financial operations is the General journal.

It allows processing virtually any type of operation:

  • ledger account transfers
  • fixed asset operations
  • customer/vendor payments
  • bank operations
  • project expenses
In this, we will demonstrate how to create a new general journal record from code.


1. In the AOT, create a new class named LedgerJournalTransData and add a create method with the following code:

public void create(boolean _doInsert = false,boolean _initVoucherList = true)
{
lastLineNum++;
journalTrans.LineNum = lastLineNum;
if (journalTableData.journalVoucherNum())
  {
        this.initVoucher(lastVoucher,false,_initVoucherList);
  }
this.addTotal(false, false);
if (_doInsert)
  {
        journalTrans.doInsert();
  }
else
  {
        journalTrans.insert();
  }
if (journalTableData.journalVoucherNum())
  {
        lastVoucher = journalTrans.Voucher;
  }
}

2. Open the LedgerJournalStatic class, and replace its newJournalTransData() method with the following code:

JournalTransData newJournalTransData(JournalTransMap _journalTrans,JournalTableData _journalTableData)

{
       return new LedgerJournalTransData(_journalTrans,_journalTableData);
}

3. On DimensionAttributeValueCombination table write a method With following Code

public static LedgerDimensionAccount getLedgerDimension(MainAccountNum _mainAccountId,
container _dimensions, container _values)
{
MainAccount                                       mainAccount;
DimensionHierarchy                             dimHier;
LedgerChartOfAccountsStructure        coaStruct;
Map                                                    dimSpec;
Name                                                  dimName;
Name                                                  dimValue;
DimensionAttribute                              dimAttr;
DimensionAttributeValue                     dimAttrValue;
List                                                     dimSources;
DimensionDefaultingEngine                  dimEng;
int i;
mainAccount = MainAccount::findByMainAccountId(_mainAccountId);
if (!mainAccount.RecId)
{
return 0;
}

select firstOnly RecId from dimHier where dimHier.StructureType ==
DimensionHierarchyType::AccountStructure && dimHier.IsDraft == NoYes::No exists join coaStructwhere coaStruct.ChartOfAccounts == LedgerChartOfAccounts::current() && coaStruct.DimensionHierarchy == dimHier.RecId;
if (!dimHier.RecId)
{
return 0;
}
dimSpec = DimensionDefaultingEngine::createEmptyDimensionSpecifiers();
for (i = 1; i <= conLen(_dimensions); i++)
{
dimName  = conPeek(_dimensions, i);
dimValue  = conPeek(_values, i);
dimAttr     = DimensionAttribute::findByName(dimName);
if (!dimAttr.RecId)
{
continue;
}
dimAttrValue = DimensionAttributeValue::findByDimensionAttributeAndValue( dimAttr, dimValue, false, true);
if (dimAttrValue.IsDeleted)
{
continue;
}
DimensionDefaultingEngine::insertDimensionSpecifer( dimSpec, dimAttr.RecId, dimValue, dimAttrValue.RecId, dimAttrValue.HashKey);
}
dimSources  =  new List(Types::Class);
dimSources.addEnd(dimSpec);
dimEng = DimensionDefaultingEngine::constructForMainAccountId( mainAccount.RecId,dimHier.RecId);
dimEng.applyDimensionSources(dimSources);
return dimEng.getLedgerDimension();
}
4. Create a new job named LedgerJournalCreate, with the following code:
static void LedgerJournalCreate(Args _args)
{
LedgerJournalTable                                                     jourTable;
LedgerJournalTrans                                                     jourTrans;
LedgerJournalTableData                                              jourTableData;
LedgerJournalTransData                                              jourTransData;
LedgerJournalStatic                                                     jourStatic;
DimensionDynamicAccount                                         ledgerDim;
DimensionDynamicAccount                                        offsetLedgerDim;
ttsBegin;
ledgerDim = DimensionAttributeValueCombination::getLedgerDimension( '110180', ['Department', CostCenter', 'ExpensePurpose'], ['OU_2311', 'OU_3568', 'Training']);

offsetLedgerDim = DimensionAttributeValueCombination::getLedgerDimension( '170150',['Department', 'CostCenter', 'ExpensePurpose'],['OU_2311', 'OU_3568', 'Training']);

jourTableData                     = JournalTableData::newTable(jourTable);
jourTable.JournalNum         = jourTableData.nextJournalId();
jourTable.JournalType         = LedgerJournalType::Daily;
jourTable.JournalName       = 'GenJrn';
jourTableData.initFromJournalName(LedgerJournalName::find(jourTable.JournalName));
jourStatic                            = jourTableData.journalStatic();
jourTransData                     = jourStatic.newJournalTransData( jourTrans, jourTableData);
jourTransData.initFromJournalTable();
jourTrans.CurrencyCode    = 'USD';
jourTrans.initValue();
jourTrans.TransDate           = systemDateGet();
jourTrans.LedgerDimension= ledgerDim;
jourTrans.Txt                     = 'General journal demo';
jourTrans.OffsetLedgerDimension = offsetLedgerDim;
jourTrans.AmountCurDebit = 1000;
jourTransData.create();
jourTable.insert();
ttsCommit;
info(strFmt("Journal '%1' has been created", jourTable.JournalNum));
}

5. Run the job and check the results by opening General ledger | Journals |
General journal













6. Click on the Lines button to open journal lines and notice the newly created line:


Posting a general journal

1. Open General ledger | Journals | General journal, and find previously created Journal or manually create a     new one. Note the journal's number.
2. In the AOT, create a new job named LedgerJournalPost with the following code

 //   (replace the text 000420_010 with the journal's number from the previous step):
static void LedgerJournalPost(Args _args)
{
   LedgerJournalCheckPost jourPost;
   LedgerJournalTable jourTable;
   jourTable = LedgerJournalTable::find('000420_010');
   jourPost = LedgerJournalCheckPost::newLedgerJournalTable( jourTable,   NoYes::Yes);
jourPost.run();
}

3. Run the job, and notice the Infolog, confirming that the journal was successfully posted:

4. Open General ledger | Journals | General journal and locate the journal to make
sure that it was posted




I-ace Technologies and Services 

No comments:

Post a Comment