I had a requirement to generate general ledger vouchers programmatically in Dynamics 365 for Operations. Since Axd classes were removed, and Data entities came into picture, I thought of using them to achieve my goal.
I tried to use LedgerJournalLineEntity but was disappointed finding that I can only use display values. Fortunately, the properties like Account and OffsetAccount exist but their access were set to Private, So, I decided to duplicate the entity to my own model and make the properties of Account and OffsetAccount public, changed the Public Entity Name, then I could use it perfectly =)
Hint: You can try the same with all kinds of journals.
class GenerateJournalVoucher
{
public void generate()
{
DimensionDynamicAccount myMainAccount;
DimensionDynamicAccount myDefaultDimension;
DimensionDynamicAccount myOffsetMainAccount;
DimensionDynamicAccount myOffsetDefaultDimension;
LedgerJournalLineGen JvGen;
JvGen.initValue();
//Common
JvGen.JournalBatchNumber = 'JB-00123'; // You can use LedgerJournalTable Entity to generate the header
// JvGen.Voucher no need to generate voucher sequence, AX will manage it for you
JvGen.CurrencyCode = 'USD';
JvGen.Text = 'Some description';
//account
JvGen.AccountType = LedgerJournalACType::Ledger;
DimensionDynamicAccount ledgerDimension = this.findOrCreateDynamicDimension(myMainAccount, myDefaultDimension);
JvGen.Account = ledgerDimension;
JvGen.DebitAmount = 1000;
//Offest account
JvGen.OffsetAccountType = LedgerJournalACType::Ledger;
DimensionDynamicAccount OffsetledgerDimension = this.findOrCreateDynamicDimension(myOffsetMainAccount, myOffsetDefaultDimension);
JvGen.OffsetAccount = ledgerDimension;
JvGen.insert();
info('JV inserted sucessfully');
}
DimensionDynamicAccount findOrCreateDynamicDimension(DimensionDynamicAccount _account, DimensionDynamicAccount _dimensions)
{
DimensionAttributeValueCombination davc;
DimensionAttributeValueSetItem dimAttValueSetItem;
DimensionAttributeValueSet dimAttValueSet;
DimensionAttributeValue dimAttValue;
DimensionAttribute dimAtt;
DimensionDynamicAccount dynamicDimension;
AccountNum account;
DefaultDimensionIntegrationValues dimensionNames;
DimensionDisplayValue dimensionValues;
davc = DimensionAttributeValueCombination::find(_account);
if (davc.LedgerDimensionType == LedgerDimensionType::DefaultAccount)
{
dimensionNames = DimensionAttribute::find(DimensionAttribute::getWellKnownDimensionAttribute(DimensionAttributeType::MainAccount)).Name;
dimensionValues = LedgerDynamicAccountHelper::getAccountNumberFromDynamicAccount(_account);
str delimiter = DimensionParameters::getDimensionSegmentDelimiter();
// Add order line dimensions
while select dimAttValueSetItem
join RecId from dimAttValueSet
where dimAttValueSet.RecId == _dimensions &&
dimAttValueSetItem.DimensionAttributeValueSet == dimAttValueSet.RecId
join RecId, DimensionAttribute from dimAttValue
where dimAttValue.RecId == dimAttValueSetItem.DimensionAttributeValue
join RecId, Name from dimAtt
where dimAtt.RecId == dimAttValue.DimensionAttribute
{
// Add the dimension name and dimension value
dimensionNames += delimiter;
dimensionValues += delimiter;
dimensionNames += dimAtt.Name;
dimensionValues += strReplace(dimAttValueSetItem.DisplayValue, delimiter, '\\' + delimiter);
}
LedgerAccountDimensionResolver ledgerAccountDimensionResolver = LedgerAccountDimensionResolver::newResolver(dimensionValues);
ledgerAccountDimensionResolver.parmDimensionFormat(dimensionNames);
dynamicDimension = ledgerAccountDimensionResolver.resolve();
}
return dynamicDimension;
}
}
*This post is locked for comments