Updating a single dimension

The dimension structure in AX 2012 has been completely redesigned. This has a lot of advantages and a few disadvantages. One of the downsides is that all we knew before we do not know anymore.

One of the things I ended up struggling a bit with was changing a single dimension value on a given dimension set. Here is a piece of code that ended up doing the trick. If you have other and/or better solution please let me know. In this case I am updating the value of a dimension called Sub Group id based on a value from the product.

    #DimensionNames // A macro that gives me the names of the dimensions
    DimensionAttributeValueSetStorage   dimStorage;
    DimensionAttribute                  dimAttribute;
    DimensionAttributeValue             dimAttributeValue;
    EcoResProductSubGroupId             subGroupId; // The value we are going to update with

    // Get the current dimension set
    dimStorage = DimensionAttributeValueSetStorage::find(this.DefaultDimension);
    // Get the attribute we want to change
    dimAttribute = DimensionAttribute::findByName(#DimName_Subproduct_group);

    // Lookup the value we should update to
    subGroupId = EcoResProductSubGroupTable::find(EcoResProduct::find(this.Product).EcoResProductSubGroupTable).SubProductGroupId;
    // If the group id is filled in add it to the dimension set. Else clear the value in the set.
    if (subGroupId)
    {
        dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttribute, subGroupId, true, true);
        dimStorage.addItem(dimAttributeValue);
    }
    else
    {
        dimStorage.removeDimensionAttribute(DimensionAttribute::findByName(#DimName_Subproduct_group).RecId);
    }

    // Add the new dimension set record id to the record
    this.DefaultDimension = dimStorage.save();

As you can see it is a bit like InventDim … just a bit more flexible. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s