1 min read

Get & Update Default Dimensions

The following example, will show how to set the value of a dimension within the default dimensions of a record (Ex: set AccountNum as Default Dimension on Vendor/Customer) in Dynamics 365 for Operations.

Make sure that your model also references Dimensions and SourceDocumentationsTypes models.
You need this because the classes & variables used , are for the moment present in both models. Ex: the ExtendedDataType for CustTable.DefaultDimension is LedgerDimensionValueSet which belongs to SourceDocumentationsTypes and DimensionValue belongs to Dimensions model.

In the example bellow i created a Pre-Handler for the update method on CustTable:

    // <summary>

    // </summary>
    // <param name="args"></param>
    [PreHandlerFor(tableStr(CustTable), tableMethodStr(CustTable, update))]
    public static void CustTable_Pre_update(XppPrePostArgs args)
    {

        CustTable           custTable = args.getThis();
        DimensionAttributeValueSetStorage dimStorage;
        DimensionAttribute dimensionAttribute;
        DimensionValue currentDimensionValue;
        Counter i;


        #define.dimensionName("Customer")

        // Build Dimensions Attribute Value Storage
        dimStorage = DimensionAttributeValueSetStorage::find(custTable.DefaultDimension);

        //Get current value
        dimensionAttribute = DimensionAttribute::findByName(#dimensionName);
        currentDimensionValue = dimStorage.getDisplayValueByDimensionAttribute(dimensionAttribute.RecId);
         
        //Update Customer Financial Dimension if it does not exist
        if(!currentDimensionValue)
        {
            dimStorage.addItem(DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, custTable.AccountNum,false,true));

 
            custTable.DefaultDimension = dimStorage.save();

            //Return updated event arguments
            args.setReturnValue(custTable);

        }
    
    
    }