Use below method to insert/update the dimension values, just pass the parameter values to the method and it will return the updated value:
public static DimensionDefault setDefaultDimension(Name _dimensionName,
DimensionValue _dimensionValue,
DimensionDefault _defaultDimension)
{
DimensionAttributeValueSetStorage dimStorage = new DimensionAttributeValueSetStorage();
DimensionAttribute dimAttribute;
DimensionAttributeValue dimAttributeValue;
DimensionValue dimensionValue;
DimensionDefault defaultDimension = _defaultDimension;
if (_dimensionValue) //If dimension value is there then insert or update it.
{
ttsBegin;
dimStorage = DimensionAttributeValueSetStorage::find(defaultDimension);
dimAttribute = DimensionAttribute::findByName(_dimensionName);
dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, _dimensionValue, true, true);
dimStorage.addItem(dimAttributeValue);
defaultDimension = dimStorage.save();
ttsCommit;
}
else if (!_dimensionValue) //If dimension value is not there then remove it.
{
ttsBegin;
dimStorage = DimensionAttributeValueSetStorage::find(defaultDimension);
dimAttribute = DimensionAttribute::findByName(_dimensionName);
dimensionValue = ProjProjectV2Entity::FindDefaultDimensionVal(_dimensionName, _defaultDimension);// This method is defined below
dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, dimensionValue, true, true);
dimStorage.removeDimensionAttributeValue(dimAttributeValue.RecId);
defaultDimension = dimStorage.save();
ttsCommit;
}
return defaultDimension;
}
/// <summary>
/// Find the dimension value
/// </summary>
/// <param name = "_dimensionName">
/// Dimension name
/// </param>
/// <param name = "_defaultDimension">
/// Default dimension value
/// </param>
/// <returns>
/// Dimension value
/// </returns>
public static DimensionValue findDefaultDimensionVal(Name _dimensionName,
DimensionDefault _defaultDimension)
{
DimensionAttributeValueSetStorage dimStorage;
DimensionValue dimensionValue;
Counter i;
dimStorage = DimensionAttributeValueSetStorage::find(_defaultDimension);
for (i= 1 ; i<= dimStorage.elements() ; i++)
{
if (DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == _dimensionName)
{
dimensionValue = dimStorage.getDisplayValueByIndex(i);
}
}
return dimensionValue;
}