How to change the value of a financial default dimension

Replace default financial dimension value
Check that financial dimension value exists:
public static DimensionAttributeValue VKValidateFinancialDimensionValue(DimensionAttribute _dimensionAttribute, str _dimensionValue, boolean _noError = false)
{
    DimensionAttributeValue dimensionAttributeValue;

    dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValueNoError(_dimensionAttribute, _dimensionValue, false, true);

    if (!dimensionAttributeValue && !_noError)
    {
        error(strFmt("@SYS342560", _dimensionValue));
    }

    return dimensionAttributeValue;
}
Get financial dimension value:
public static DimensionValue VKGetFinancialDimensonValue(DimensionDefault _dimensionDefault, Name _dimensionName)
{
    DefaultDimensionView defaultDimensionView;
    
    select firstonly DisplayValue
    from defaultDimensionView
    where defaultDimensionView.Name == _dimensionName
        && defaultDimensionView.DefaultDimension == _dimensionDefault;

    return defaultDimensionView.DisplayValue;
}
Change/replace financial dimension value:
public static DimensionDefault ChangeFinancialDimensionValue(DimensionDefault _dimensionDefault, Name _dimensionName, DimensionValue _newDimensionValue)
{
    DimensionAttributeValueSetStorage   dimensionAttributeValueSetStorage;
    DimensionAttribute                  dimensionAttribute;
    DimensionValue                      oldDimensionValue;
    DimensionDefault                    newDimensionDefault;

    // Get current value
    oldDimensionValue = VKFunctions::VKGetFinancialDimensonValue(_dimensionDefault, _dimensionName);

    // Build DimensionAttributeValueSetStorage
    dimensionAttributeValueSetStorage = DimensionAttributeValueSetStorage::find(_dimensionDefault);

    // Remove old dimension value
    dimensionAttribute = DimensionAttribute::findByName(_dimensionName);
    dimensionAttributeValueSetStorage.removeDimensionAttributeValue(
        DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, oldDimensionValue).RecId);

    // Set new dimension value
    if(_newDimensionValue)
    {
        dimensionAttributeValueSetStorage.addItem(
            DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, _newDimensionValue, false, true));
    }

    newDimensionDefault = dimensionAttributeValueSetStorage.save();

    return newDimensionDefault;
}
Usage:
AssetBook            assetBook;
DimensionDefault newDimensionDefault = assetBook.DefaultDimension;
newDimensionDefault = VKChangeFinancialDimensionValue(newDimensionDefault, 'ALegalEntity', '010');
Compose finantial dimension value:
    static LedgerDefaultDimensionValueSet VKGenerateDefaultDimension(str _legalEntity, str _workOrderNumber, str _workItemType)
    {
        List dimensionValues = new List(Types::Class);

        DimensionNameValueListContract  dimensionNameValueListContract      = new DimensionNameValueListContract();
        DimensionStorageResult          dimensionStorageResult;

        if (!_legalEntity)
        {
            _legalEntity = curExt();
        }

        DimensionAttributeValueContract dimensionAttributeValueContractLE   = DimensionAttributeValueContract::construct('ALegalEntity', _legalEntity);
        dimensionValues.addEnd(dimensionAttributeValueContractLE);

        if (_workOrderNumber)
        {
            DimensionAttributeValueContract dimensionAttributeValueContractWON  = DimensionAttributeValueContract::construct('IWorkOrderNumber', _workOrderNumber);
            dimensionValues.addEnd(dimensionAttributeValueContractWON);
        }

        if (_workItemType)
        {
            DimensionAttributeValueContract dimensionAttributeValueContractWIT  = DimensionAttributeValueContract::construct('JWorkItemType', _workItemType);
            dimensionValues.addEnd(dimensionAttributeValueContractWIT);
        }

        dimensionNameValueListContract.parmValues(dimensionValues);
        dimensionStorageResult = DimensionNameValueListServiceProvider::newForDimensionNameValueListContract(dimensionNameValueListContract).resolve();

        return dimensionStorageResult.parmSavedRecId();
    }
Usage:
        inventJournalTrans.DefaultDimension = VKGenerateDefaultDimension('010', 'work order 1', 'work item 1');
Other helper classes, which contain static methods to help with finantial dimensions:
DimensionDefaultingService
AxdDimensionUtil
DimensionDefaultingEngine
DimensionAttribute - table with all financial dimension names DimensionAttribute.Name

 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units.
Join us.

Blog Tags