Thursday, September 1, 2011

Ax 2012 - Get dimensions

If you've been working with Dynamics AX 2012 dimensions, you'll know that they are pretty hard to understand and work until you get to know them. I finally figured out how to get the dimensions and dimension values through X++.

Get dimensions:

DimensionAttribute              dimAttr;
DimensionAttributeSetItem       dimAttrSetItem;
DimensionAttributeSetItem       dimAttrSetItemEditable;
DimensionEnumeration            dimensionSetId;
dimensionSetId = DimensionCache::getDimensionAttributeSetForLedger();
while select dimAttr
    order by Name
       where dimAttr.Type != DimensionAttributeType::MainAccount
        join RecId from dimAttrSetItem
       where dimAttrSetItem.DimensionAttribute == dimAttr.RecId &&
             dimAttrSetItem.DimensionAttributeSet == dimensionSetId
  outer join RecId from dimAttrSetItemEditable
       where !dimAttrSetItemEditable.DimensionAttributeSet &&
             dimAttrSetItemEditable.DimensionAttribute == dimAttr.RecId &&
             dimAttrSetItemEditable.EnumerationValue == NoYes::Yes
{
   info(dimAttr.Name);
}
Get dimension values for the "CostCenter" dimension:
Common common;
DimensionAttribute dimAttr;
select firstOnly dimAttr where dimAttr.Name == "CostCenter";
common = new DictTable(dimAttr.BackingEntityType).makeRecord();
while select common
{
    info(strFmt("%1", common.(dimAttr.NameAttribute)));
}