Friday 26 March 2021

Create Default Dimension using cost centre and it's Derived Dimensions in D365 FSCM X++

 Pass the CostCentre value in the parameter to create the default dimension and fill up other dimension values from cost centre's derived dimensions:


Below is the code, pass the cost Centre value as a parameter.

public static DimensionDefault QTQ_CreateDerivedDimension(String20 _costCentre)
    {
        DimensionAttributeValue dimAttrValue;
        DimensionAttribute      dimAttr;
        str                     dimAttrCCValue;
        DimensionDefault        result;
        boolean                 isDerivedDimension;
                            
        dimAttrCCValue          = _costCentre;
        
        dimAttr                 = DimensionAttribute::findByName("CostCentre");//CustParameters::find().QTQ_DimAttName);
        dimAttrValue            = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttr, dimAttrCCValue, false, true);
                        
        DimensionAttributeValueDerivedDimensions    derivedDim = DimensionAttributeValueDerivedDimensions::findByDimensionAttributeValue(dimAttrValue.DimensionAttribute, dimAttrValue.RecId);
        DimensionAttributeValueSetStorage           defaultDimStorage = new DimensionAttributeValueSetStorage();
        DimensionHierarchy                          dimHierarchy = DimensionAttributeDerivedDimensions::findDimensionHierarchyForDrivingDimension(DimensionAttribute::find(derivedDim.DimensionAttribute));
        DimensionHierarchyLevel                     dimHierarchyLevel;
        DimensionAttributeDerivedDimensions         derivedDimensions;

        while select RecId, DimensionAttribute from dimHierarchyLevel
                                where dimHierarchyLevel.DimensionHierarchy == dimHierarchy.RecId
                                    join DerivedDimensionFieldNum from derivedDimensions
                                        where derivedDimensions.DimensionHierarchyLevel == dimHierarchyLevel.RecId
        {
            DimensionAttributeValueRecId    davRecId = derivedDim.(derivedDimensions.DerivedDimensionFieldNum);
            DimensionAttributeValue         dav = DimensionAttributeValue::find(davRecId);

            defaultDimStorage.addItemValues(dimHierarchyLevel.DimensionAttribute, dav.RecId, dav.HashKey);
            isDerivedDimension = true;
        }

        // If there is no derived dimension available then at-least create the default dimension with Cost Centre Value only.
        if (!isDerivedDimension)
        {
            defaultDimStorage.addItem(dimAttrValue);
        }
        result = defaultDimStorage.save();
        return result; 
    }


Insert/Update or remove the default dimension value in D365 FSCM via x++

Use below method to insert/update the dimension values, just pass the parameter values to the method and it will return the updated value: p...