Friday, July 13, 2018

How to find or create default Dimension from X++ in AX 2012


How to find or create default Dimension from X++ in AX 2012


Following is the code you can use to find or create dimension in AX 2012


static DimensionDefault generateDefaultDimension(container _attr, container _value, boolean _createIfNotFound = true)
    {
        DimensionAttributeValueSetStorage valueSetStorage = new DimensionAttributeValueSetStorage();
        DimensionDefault result;
        int i;
        DimensionAttribute dimensionAttribute;
        DimensionAttributeValue dimensionAttributeValue;
        //_attr is dimension name in table DimensionAttribute
        container conAttr = _attr;
        container conValue = _value;
        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 != "")
            {
                // _createIfNotFound is "true". A dimensionAttributeValue record will be created if not found.
                dimensionAttributeValue=
                dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,_createIfNotFound);

                // Add the dimensionAttibuteValue to the default dimension
                valueSetStorage.addItem(dimensionAttributeValue);
            }
        }
        result = valueSetStorage.save();
        return result;
    }

No comments:

Post a Comment