How to merge financial dimensions via code in Microsoft Dynamics 365
Merging financial dimensions is a common task that a developer may need to perform via x++ code in Microsoft Dynamics 365. This blog will demonstrate two different approaches for merging financial dimensions. The first example is using a standard business process. The second example is a custom example where we will look at an alternative way to merge default dimensions. Additionally, we will discuss some limitations and expected results when merging financial dimensions in D365.
Example of a Standard Process
A great example that exists in the system is when a sales line is initially created. By default, the line will copy the sales header default dimension. Once an item is specified, the sales line will be updated with a financial dimension that merges the value of the sales line and the item’s default dimension. Below you can see the method on the sales line that performs this operation.
public DimensionDefault mergeDimension(
DimensionDefault _primaryDefaultDimension,
DimensionDefault _secondaryDefaultDimension = 0,
DimensionMerge _dimensionMerge =DimensionMerge::newFromTable(this,
this.companyInfo().RecId))
{
return _dimensionMerge.merge(_primaryDefaultDimension,_secondaryDefaultDimension);
}
The method above takes in the two separate DimensionDefault parameters to merge and create a new DimensionMerge object instance with the basis being sales line. In order to instantiate your own DimensionsMerge object, you will need to pass in an instance of the table you are modifying that has a mapping to the DimensionDefaultMap, as well as the recId of the company the financial dimension belongs to. Once you have that object you may simply call the merge method passing in the two DimensionDefault fields to be merged. Note that the DimensionDefault data type is a 64-bit integer that references a DimensionAttributeValueSet record.
Hidden Treasures of Advanced Warehousing
Make the most of Microsoft Dynamics 365’s Advanced Warehousing module and gain insight into the hidden treasures to unlock value.
Example of a Custom Merge
There is a second way of merging default dimensions that can be used. Using this option can be handy if all you have are the default dimension values which do not utilize the DimensionDefaultMap object. In this example, we still pull our values from table buffers, but they are not required. Below is a snippet of code that demonstrates this.
public void mergeFinDim()
{
SalesLine salesLine;
InventTable inventTable;
//USMF data
salesLine = SalesLine::find('001012', 1, true);
inventTable = InventTable::find('A0001');
salesLine.DefaultDimension =
LedgerDimensionDefaultFacade::serviceMergeDefaultDimensions(
salesLine.DefaultDimension, inventTable.DefaultDimension);
}
In this method of merging financial dimensions, you do not need any other objects other than at least two DimensionDefault values. You should ensure that the dimension values that are passed into the service method are in priority order.
The merged dimension is initialized from the first supplied default dimension values. Subsequent dimension values are added to the merged dimension if they are blank in the merged dimension. Below is a table demonstrating the inputs and outputs of the custom merge example:
Default Dimension | Business Unit | Cost Center | Department | Item Group |
Sales Line 1 | 001 | Trade Show | Sales | |
Item A0001 | Production | HR | Audio | |
Merged Dimension | 001 | Trade Show | Sales | Audio |
If you are using the LedgerDimensionDefaultFacade class in your solution, you will need to reference the Ledger and Dimension packages in your model.
In this blog, we have demonstrated how a developer can merge default dimensions. We demonstrated two separate approaches with examples and discussed key points the developer should be aware of when using these tools.
Deep Expertise in Dynamics AX and D365 Development
enVista is dedicated to helping the Microsoft community by continuously sharing our in-depth knowledge of X++ and Microsoft Dynamics 365 development. If you are struggling with Dynamics AX or D365 development, contact us, and we will connect you with one of our in-house experts.
Read our other topics in our D365 Developer Series:
Extending Full-Text within Retail and Commerce