8/14/2019

Build default dimension and ledger dimension in D365fo

It's pretty common to build default dimension or ledger dimension with dimension name and value, especially when developing transaction related functionalities. In D365fo, the dimension utility classes are different than the AX ones, so I created this post as a memo, and if it could help others that will be even better.

1. Build default dimension

public static RecId GetDefaultDimension(Map _dimNameValueMap)
{
MapEnumerator           mapEnum;
//Map                      dimNameValueMap = new Map(Types::String, Types::String);
RecId                    defaultDimension;        
DimensionAttribute       dimAttribute;
DimensionAttributeValue  dimAttributeValue;
DimensionAttributeValueSetStorage dimAttributeValueSetStorage = new DimensionAttributeValueSetStorage();
   
mapEnum = _dimNameValueMap.getEnumerator();

while(mapEnum.moveNext())
{
str     dimName, dimValue;
dimName      = mapEnum.currentKey();
dimValue     = mapEnum.currentValue();
// find and validate the dimension name.
dimAttribute = DimensionAttribute::findByName(dimName);
if (dimAttribute.RecId == 0)
{
error(strFmt('Dimension {0} doesn\'t exist.', dimName));
}

// find the dimension attribute value and append it to storage.
if (dimValue)
{
dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, dimValue, false, true);
dimAttributeValueSetStorage.addItem(dimAttributeValue);
}
else
{
error(strFmt('Dimension {0} doesn\'t have value.', dimName));
}
}

defaultDimension = dimAttributeValueSetStorage.save();
return defaultDimension;
}


2. Build ledger dimension with main account and default dimension

public static RecId GetLedgerDimension(MainAccountNum _mainAccountId, RecId _defaultDimension)
{
MainAccount     mainaccount = MainAccount::findByMainAccountId(_mainAccountId);
RecId      ledgerddimension;

LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId(_mainAccountId);
ledgerddimension = LedgerDimensionFacade::serviceCreateLedgerDimension(LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId(_mainAccountId), _defaultDimension);

return ledgerddimension;
}



3. Get display value of a specific dimension name from ledger dimension

public staic str GetDisplayValue(RecId _ledgerDimension, str _dimensionName)
{
    DimensionAttributeLevelValueView dimAttributeLevelValueView;
    DimensionAttribute dimAttribute = DimensionAttribute::findByName(_dimensionName);
    select DisplayValue from dimAttributeLevelValueView  where dimAttributeLevelValueView.ValueCombinationRecId == _ledgerDimension && dimAttributeLevelValueView.DimensionAttribute == dimAttribute.RecId;
    return dimAttributeLevelValueView.DisplayValue;
}

No comments:

Post a Comment