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.