Fetching Mandatory Default Dimensions from Account Structure in D365 F&O

 

Fetching Mandatory Default Dimensions from Account Structure in D365 F&O

In Dynamics 365 Finance & Operations (D365 F&O), Account Structures and Advanced Rules together determine which financial dimensions are required, optional, or restricted during transaction posting. In this context, I am attempting to retrieve the mandatory financial dimensions using X++.

 1. Business Requirement
When a user selects a Main Account, the system should:
  • Identify the applicable Account Structure
  • Evaluate Advanced Rules
  • Return only mandatory financial dimensions

2. Conceptual Understanding

🔹 What Drives Mandatory Dimensions?

Mandatory dimensions are derived from:

  • Account Structure
  • Advanced Rule Structure
  • Constraint Nodes (IsOptional flag)
X++ Implementation

class MVDDimensionRuleCriteria
{
    public static container getMaxDimensionRule(MainAccountNum _mainaccount)
    {
        // Table declarations
        DimensionHierarchy                dh, dh2;
        DimensionHierarchyLevel           dhl;
        DimensionAttribute                da;
        DimensionRuleAppliedHierarchy     drah;
        DimensionRule                     dr;
        DimensionRuleCriteria             drc;
        DimensionConstraintNode           dcn;
        LedgerAdvancedRuleCriterionEntity ledE;
        MainAccount                       ma;

        // Variables to track best rule
        Map     ruleCountMap = new Map(Types::Int64, Types::Integer);
        int     maxCount     = 0;
        int     currentCount;
        RecId   bestRuleRecId;

        container mandatoryDimContainer; // Final output container


        // ============================================================
        // STEP 1: Identify the best matching dimension rule
        // Logic: Count occurrences of each rule and pick the one with max matches
        // ============================================================
        while select dh
            where dh.StructureType == 1

        join dhl
            where dhl.DimensionHierarchy == dh.RecId

        join da
            where da.RecId == dhl.DimensionAttribute

        join drah
            where drah.DimensionHierarchy == dh.RecId

        join dr
            where dr.RecId == drah.DimensionRule

        join dh2
            where dh2.RecId == dr.AccountStructure
               && dh2.Name  == "Account Structure-JAPL"

        join drc
            where drc.DimensionRule == dr.RecId

        outer join dcn
            where dcn.DimensionHierarchyLevel == dhl.RecId

        join ledE
            where ledE.RecId == drc.RecId

        join ma
            where ma.MainAccountId == _mainaccount
               &&
               (
                   (drc.IsFromOpen && ma.MainAccountId >  drc.RangeFrom) ||
                   (!drc.IsFromOpen && ma.MainAccountId >= drc.RangeFrom)
               )
               &&
               (
                   (drc.IsToOpen && ma.MainAccountId <  drc.RangeTo) ||
                   (!drc.IsToOpen && ma.MainAccountId <= drc.RangeTo)
               )
        {
            // Get current count for this rule
            currentCount = ruleCountMap.exists(dr.RecId)
                         ? ruleCountMap.lookup(dr.RecId)
                         : 0;

            currentCount++;

            // Update map with new count
            ruleCountMap.insert(dr.RecId, currentCount);

            // Track rule with maximum matches
            if (currentCount > maxCount)
            {
                maxCount       = currentCount;
                bestRuleRecId  = dr.RecId;
            }
        }


        // ============================================================
        // STEP 2: Collect mandatory dimensions from the best rule
        // Logic: Only include dimensions where IsOptional = No
        // ============================================================
        while select dh
            where dh.StructureType == 1

        join dhl
            where dhl.DimensionHierarchy == dh.RecId

        join da
            where da.RecId == dhl.DimensionAttribute

        join drah
            where drah.DimensionHierarchy == dh.RecId

        join dr
            where dr.RecId == drah.DimensionRule
               && dr.RecId == bestRuleRecId

        join dh2
            where dh2.RecId == dr.AccountStructure
               && dh2.Name  == "Account Structure-MVD" // Optional: filter by specific structure

        join drc
            where drc.DimensionRule == dr.RecId

        outer join dcn
            where dcn.DimensionHierarchyLevel == dhl.RecId

        join ledE
            where ledE.RecId == drc.RecId

        join ma
            where ma.MainAccountId == _mainaccount
        {
            // Include only mandatory dimensions
            if (dcn.RecId && dcn.IsOptional == NoYes::No)
            {
                mandatoryDimContainer += da.Name;
            }
        }

        // Return final list of mandatory dimension names
        return mandatoryDimContainer;
    }
}


Comments

Popular posts from this blog

How to Retrieve All Vendor Location IDs in X++ (Beyond Primary Addresses) in Microsoft Dynamics 365 Finance & Operations

Product Tax Rate Search Report (HSN & SAC) in D365 F&O