Get a cost center in D365FO

Get a cost center in D365FO

It is shocking to see over and over again functional consultants failing to create the Cost center dimension backed by the Organization units table (Organisation administration > Organisations > Operating units), choosing a custom dimension type instead and loosing the ability to specify an address, make a hierarchy of cost centers, specify a cost center manager etc.

This has also given a rise to excruciatingly dumb custom code snippets predating the dimension name CostCenter as if there were no British English or other languages.

With the proper setup and the proper coding this reduces to 3 essential lines:

				
					static void main(Args _args)
{
    DefaultDimensionView defaultDimensionView;
    RefRecId testDefaultDimensionRecId = (select firstonly DefaultDimension from ProdTable where ProdTable.ProdId == "P000173").DefaultDimension;
    
    // Copy of the private method
    OMOperatingUnit::getDimensionViewId()
    RefTableId getDimensionViewId(OMOperatingUnitType _omOperatingUnitType)
    {
        Dictionary dict = new Dictionary();
        DictEnum dictEnum = new DictEnum(enumNum(OMOperatingUnitType));
        return dict.tableName2Id(strFmt('DimAttribute%1', dictEnum.index2Symbol(_omOperatingUnitType)));
    }

    // Filter by the virtual backing entity table Id for cost centers
    select defaultDimensionView
        where defaultDimensionView.BackingEntityType == getDimensionViewId(OMOperatingUnitType::OMCostCenter)
           && defaultDimensionView.DefaultDimension == testDefaultDimensionRecId;
    
    info(defaultDimensionView.DisplayValue);
}