Skip to main content
Just needed to save some code I have been working on in my blog.

This code is used for basically deconstructing ledger dimensions and default dimensions to be able to merge / overwrite dimension attribute values in the ledger dimension.

My scenario is that during the processing of a ledger allocation, I have an "original" transaction that is being allocated. My destination transaction (the transaction that allocates the original transaction to a new ledger dimension) will inherit all the ledger dimensions of the original transaction BUT will have some of it's dimension attribute values overwritten, by a default dimension set up on the ledger allocation rule destination.

Basically I deconstruct the ledger dimension of the original transaction and put all the found dimensions into a map.

I the deconstruct the default dimension of the ledger allocation rule destination, and find the matching entries in my map, to replace them.

When my "merged" map is complete, I prepare a list of DimensionAttributeValueContract objects that is fed in to a LEdgerAccountContract object.

This is then used when calling DimensionsServiceProvider::BuildDimensionStorageForLedgerAccount to create a DimensionAttributeValueCombination record that can be attached on my LedgerJournalTrans record in the LedgerDimension field.



///
/// Finds or creates a DimensionAttributeValueCombination record

///

///

/// GeneralJournalAccountEntry record

///

///

/// LedgerAllocationRuleDestination

///

///

/// Recid of DimensionAttributeValueCombination record

///

///

/// Used to ensure that the correct main account / dimension combination for allocation ledgerJournalTrans are available

/// The combination is constructed using dimensions inherited from the "original" generealAccountJournalEntry record that is being allocated

/// and the default dimension of the ledgerallocationruleDestination. The dimensions from the ledgerAllocationRuleDestination overrides the

/// dimensions of the original generealAccountJournalEntry if set.

///


private recId findOrCreateDimAttrValueCombination(LedgerJournalTrans _sourceTrans,LedgerAllocationRuleDestination _ledAllocationRuleDest)


{

// These objects are used for constructing a ledgerdimension

LedgerAccountContract ledAccountContract;

DimensionAttributeValueContract dimAttrValueContract;

List dimAttrValueObjects;

Map commonAttributeMap;

MapIterator commonAttributeMapInterator;


DimensionStorage dimStorage;

// Deconstruction variables ledgerdimension

int hierachyCount,hierachyIndex;

int segmentCount,segmentIndex;

DimensionStorageSegment dimStorageSegment;

DimensionAttribute orgTransDimAttribute;

DimensionAttributeValue orgTransDimAttributeValue;


// Deconstruction variables defaultdimension for ledgerallocationruledestination

DimensionAttributeValueSetStorage dimAttrValueSetStorage;

int ruleDestCount,ruleDestIndex;

// Common attribute map object

commonAttributeMap = new Map(Types::Int64,Types::Int64);


// Construct dimensions storage object for desconstructing dimension values of original trans

dimStorage = DimensionStorage::findById(_sourceTrans.LedgerDimension);


hierachyCount = dimStorage.hierarchyCount();



// Deconstruction original trans and constructing a List of dimAttrValueContract objects that can be used for finding/creating a

// DimensionAttributeValueCombination record

for(hierachyIndex=1;hierachyIndex<=hierachyCount;hierachyIndex++)

{

segmentCount = dimStorage.segmentCountForHierarchy(hierachyIndex);

for(segmentIndex=1;segmentIndex<=segmentCount;segmentIndex++)

{

// Get segment

dimStorageSegment = dimStorage.getSegmentForHierarchy(hierachyIndex,segmentIndex);

if (dimStorageSegment.parmDimensionAttributeValueId() != 0)

{

orgTransDimAttribute = DimensionAttribute::find(DimensionAttributeValue::find(dimStorageSegment.parmDimensionAttributeValueId()).DimensionAttribute);

orgTransDimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(orgTransDimAttribute,dimStorageSegment.parmDisplayValue());

commonAttributeMap.insert(orgTransDimAttribute.RecId,orgTransDimAttributeValue.RecId);

}

}

}



// Deconstruction ledger allocation rule destination and overriding dimensionattribute values for original transaction dimension with the default dimension

// from the allocation rule destination



// Construct DimensionAttributeValueSetStorage object for deconstructing dimension values of ledgerAllocationRuleDestination

dimAttrValueSetStorage = DimensionAttributeValueSetStorage::find(_ledAllocationRuleDest.ToDefaultDimension);

ruleDestCount = dimAttrValueSetStorage.elements();


// Traverse DimensionAttributeValueSet and "override" values in common attribute map

for(ruleDestIndex=1;ruleDestIndex<=ruleDestCount;ruleDestIndex++)

{

if (commonAttributeMap.exists(dimAttrValueSetStorage.getAttributeByIndex(ruleDestIndex)))

{

// Remove dimension already gathered

commonAttributeMap.remove(dimAttrValueSetStorage.getAttributeByIndex(ruleDestIndex));

// Insert overridden dimension

commonAttributeMap.insert(dimAttrValueSetStorage.getAttributeByIndex(ruleDestIndex),dimAttrValueSetStorage.getValueByIndex(ruleDestIndex));

}

else

commonAttributeMap.insert(dimAttrValueSetStorage.getAttributeByIndex(ruleDestIndex),dimAttrValueSetStorage.getValueByIndex(ruleDestIndex));

}



// Build list of DimensionAttributeValueContract objects

// Initialize list of DimensionAttributeValueContract objects

dimAttrValueObjects = new List(Types::Class);

commonAttributeMapInterator = new MapIterator(commonAttributeMap);

// Traverse "common set" of attributes for main account

while (commonAttributeMapInterator.more())

{

// Prepare DimensionAttributeValueContract objects for DimensionServiceProvider object

dimAttrValueContract = new DimensionAttributeValueContract();



orgTransDimAttribute = DimensionAttribute::find(commonAttributeMapInterator.key());

orgTransDimAttributeValue = DimensionAttributeValue::find(commonAttributeMapInterator.value());



// Inherit dimension from original transaction

dimAttrValueContract.parmName(orgTransDimAttribute.Name);

dimAttrValueContract.parmValue(orgTransDimAttributeValue.getValue());

dimAttrValueObjects.addEnd(dimAttrValueContract);

commonAttributeMapInterator.next();

}



// Initialize LedgerAccountContract

ledAccountContract = new LedgerAccountContract();

ledAccountContract.parmMainAccount(DimensionStorage::getMainAccountFromLedgerDimension(_sourceTrans.LedgerDimension).MainAccountId);

ledAccountContract.parmValues(dimAttrValueObjects);

return DimensionServiceProvider::buildDimensionStorageForLedgerAccount(ledAccountContract).save();

}

As always - If you use this code, you do so at your own risk.

Comments

Popular posts from this blog

Suppressing the infolog

Supressing the infolog is often useful in D365FO when augmenting code. When augmenting code using COC (Chain Of Command), you can have new code run either before or after the code you are augmenting. This means that any infolog-messages that the standard application code does, will be shown to the user, even if your augmentation supports a scenario where there must be no infolog-messages. How do you avoid the standard application infolog-messages ? To the rescue comes temporary supression of the infolog. The suppression consists of: 1) Saving the current infologLevel 2) Setting the infologLevel to SysInfologLevel::None 3) Run your code 4) Restoring the saved infologLevel to the infolog For example a table could have a validatewrite-method that validates that you are only allowed to use 3 out of 6 options in an enum-field, and you need to allow for a fourth one. Table a - validateWrite method: boolean validateWrite() {     Switch (this.enumField)     {         boolean ret;         case

Dynamics ax 2012 traversing selected records in a form data source

A classical developer challenge in Dynamics AX is to enable a form button when multiple records have been selected in the form by the user. This usually involves writing some form of loop (for or while or do-while) that starts out with calling _ds.getFirst() and continuing the loop as long as _ds.getNext() returns a tablebuffer. Well things got a little bit easier in AX 2012. In AX 2012 you can use the MultiSelectionHelper class. One example is the following that I encountered in AX 2012: Can you make the customer collection letter print out run for each selected collection record in the Print/Post collection letters form (Accounts receiveable / Periodic / Collections / Print/Post Collection letters). If we ignore the possibility for setting up print destination for running each report we can do this in two steps: 1) Change the "Multiselect" property of the "MenuButton" and the "Menuitembutton" in the MenuButton in the form from "Auto&quo

Indicating mandatory field in a dialog (RunBase) class.

A classical problem is indicating that a field is mandatory in a dialog, when the field is not bound to a datasource/field in a datasource. Normally fellow developers will tell you that, that is not possible. I found a way to do this. In your Runbase-based class you can implement the putToDialog-method e.g like this: protected void putToDialog() { super(); fieldMask.fieldControl().mandatory(true); } where fieldMask is a DialogField object in your dialog. This will make the field act like it was a mandatory field from a datasource in a form, showing a red-wavy line under the field, and requiring the field to have a value. Attention: Your class has to run on the client.If you set your class to run on the server, you get a run-time error, when the fieldMask.FieldControl()-call is made.