Updating Default dimensions -- the best possible way
Even a few versions of D365FO this, was damn easy. There was this function, which can let you change twitch between any Fin. dimension values:
ledgerDimensionDefaultFacade::serviceReplaceAttributeValue(SalesLine.DefaultDimension, InventTable.DefaultDimension, DimensionAttribute::findByName("ProdLine").RecId);
But this method has changed and it looks like the following:
dimension = LedgerDimensionDefaultFacade::serviceReplaceAttributeValue(
_defaultDimension,
_dimensionDefaultMap.DefaultDimension,
dimensionAttributeRecId);
Whereby you have to generate a new dimension and then assign it to the original dimension.
Oh Rama....there could be sheer chances of again the structure of this method getting changed and we have to refurbish our code again.
Here is a pretty easy-to-use code, that can help you anytime to replace any dimension value with a new one -- regardless of whatever changes happen to any of these underlying Dimension classes.
private DimensionDefault updateDefaultDimension(SalesLine _salesLine, InventLocationId _newLocationVal)
{
DimensionDefault fromDimensionDefault = _salesLine.DefaultDimension,
toDimensionDefault;
DefaultDimensionView defaultDimensionView;
Map mapDimvalues = new Map(Types::String, Types::String);
Counter repeats = 1;
while select DisplayValue, Name from defaultDimensionView
order by RecId asc
where defaultDimensionView.DefaultDimension == fromDimensionDefault
{
if (repeats == 1)
{
mapDimvalues.insert(defaultDimensionView.Name, _newLocationVal);
}
else
{
mapDimvalues.insert(defaultDimensionView.Name, defaultDimensionView.DisplayValue);
}
repeats ++;
}
DimensionAttributeValueSetStorage valueSetStorage = new DimensionAttributeValueSetStorage();
MapEnumerator dimValEnum = mapDimvalues.getEnumerator();
DimensionAttributeValue dimensionAttributeValue;
while (dimValEnum.moveNext())
{
DimensionAttribute dimensionAttribute = dimensionAttribute::findByName(dimValEnum.currentKey());
if (dimensionAttribute.RecId == 0)
{
continue;
}
DimensionValue dimValue = dimValEnum.currentValue();
if (dimValue != "")
{
dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, dimValue, false, true);
valueSetStorage.addItem(dimensionAttributeValue);
}
}
toDimensionDefault = valueSetStorage.save();
return toDimensionDefault;
}
Here, as per our business need, I needed to change the first dimension (i.e.: cost-center from CostCenter-Purpose-Department-Employee-Region combination) with a new code. That's what the 'if (repeats == 1)' check is doing. You can change it to any value you want, depending on the which dimension value you want to update (3rd/4th/5th and so on). The all-time favorite 'DefaultDimensionView ' holds all the records of any given dimension combination (field DefaultDimension) which we can loop through as per our requirement.
Comments
Post a Comment