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)
{
// 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)));
}
DefaultDimensionView defaultDimensionView;
RefRecId testDefaultDimensionRecId =
(select firstonly DefaultDimension from ProdTable where ProdTable.ProdId == "P000173").DefaultDimension;
// 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);
}
X++ programming blog series
Further reading:
Print a custom product label: a Template solution in Process Guide Framework, with detours
Extending SysOperation contracts with DataMemberAttribute
X++ Decorator pattern in Dynamics 365
Get a cost center in D365FO
Find and merge a DimensionAttributeValue
SysExtension framework pitfall: avoid new()
Input validation and messaging in the Process Guide Framework