Create Default Dimensions using X++ code in AX 2012

In Dynamics AX, there are default dimensions usually made up of Department, Cost Center and other dimensions. At times when you are creating Sales Order or Purchase Order you create lines and each line has default dimensions. Those default dimensions are not so straight forward. They require some understanding. For example, I am sharing a screen shot of a Sales Order and the sales line having a default dimension just to show you how does it look like.

 

 

Now if you have to create default dimensions using X++ you will have to create DimensionAttributeValueSetStorage record. Here is the code for creating the default dimension. In my case, I needed cost center, department and product category. You can add as many dimensions in this method.

private DimensionDefault createDefaultDimension(str costcenter, str department, str productCat)
{
    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
    DimensionDefault                    result;

    int                     i;
    DimensionAttribute      dimensionAttribute;
    DimensionAttributeValue dimensionAttributeValue;

    container               conAttr = ["Department","Center", "ProductCategory"];
    container               conValue = [department, costcenter, productCat];
    str                     dimValue;

    for (i = 1; i <= conLen(conAttr); i++)
    {
        dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));

        if (dimensionAttribute.RecId == 0)
        {
            continue;
        }

        dimValue = conPeek(conValue,i);

        if (dimValue != "")
        {
            // The last parameter is "true". A dimensionAttributeValue record will be created if not found.
            dimensionAttributeValue =
                    dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);

            // Add the dimensionAttibuteValue to the default dimension
            valueSetStorage.addItem(dimensionAttributeValue);
        }
    }

    result = valueSetStorage.save();
    return result;
}

 

You can use the above code in a method or directly wherever you need to create default dimension.

Happy Daxing. Keep following me for AX technical stuff.

 

Abubaker Siddiq Shekhani

Abubaker Shekhani is an IT Entrepreneur and Full Stack Developer. He is the co-founder and the Developer behind Mytabeeb, a health care solution. He has worked for 5 years in Microsoft Dynamics AX space with Techno-functional role and glad to be one of few Microsoft Dynamics AX developers/consultants in Pakistan. He is TEDx speaker and likes to speak in public. He is an Amateur Astronomer and Astrophotographer. He is the founding member of Karachi Astronomers Society. He can be reached at me@abubakershekhani.com.

You may also like...