How to set a single dimension value in AX 2012?

Jan B. Kjeldsen picture Jan B. Kjeldsen · Jan 24, 2012 · Viewed 44.4k times · Source

My problem is setting some dimension values read from an external source.

Given the AX 2009 statement:

ledgerJournalTrans.Dimension    = ledgerTable.Dimension;
ledgerJournalTrans.Dimension[1] = "abc";

What is the equivalent way to that in AX 2012?

This will of cause assume the existence of a "Department" dimension as the first dimension.

Answer

dlannoye picture dlannoye · Jan 25, 2012

First things first, the legacy dimensions in AX 2009 have been completely redone and replaced in AX 2012. The new LedgerDimension are a combination of the account and the old dimension values that are needed based on the account structure and advanced rule structures. To find out more about the new Dimension Framework in AX 2012 try reading the whitepaper about the new dimension framework.

Assuming that Department is the first dimension and that both dimensions are using the LedgerDimensionAccount EDT simplifies this scenario quite a bit, but the new dimension framework is very flexible so this assumption may not be correct. Even if it is, simply specifying a new value for Department may require that the structure of the combination needs to change drastically because of advanced rule structures that can be setup.

Honestly, I would only consider the code below as just a demonstration how some of the basics work, not something that should be used in production. With that said, this code should accomplish what you want, if that Dimension field on LedgerTable has be replaced with a LedgerDimension field using the LedgerDimensionAccount EDT which stores a full combination.

DimensionStorage dimensionStorage;
DimensionAttribute deparmentDimensionAttribute;
DimensionAttributeValue newDepartmentValue;

// Find the deparment dimension attribute
deparmentDimensionAttribute = DimensionAttribute::findByName("Department");  

// Find the new department value we want to put in the new combination.
newDepartmentValue = DimensionAttributeValue::findByDimensionAttributeAndValue(deparmentDimensionAttribute, "abc");

// Load a DimensionStorage instance with the old combination and update the first segment with new value
dimensionStorage = DimensionStorage::findById(ledgerTable.LedgerDimension);
dimensionStorage.setSegment(1,DimensionStorageSegment::constructFromValue("abc", newDepartmentValue));
ledgerJournalTrans.LedgerDimension = dimensionStorage.save();