Search code examples
axaptax++dynamics-ax-2012

How do I get the account structure for a particular Main Account in AX 2012?


I want to get the account structure (particularly the dimension names) for an AX Main Account that you can create in General Ledger --> Setup --> Account structures, for the purpose of dynamically creating input boxes in a webpage.

The account structures form is pretty complex, and so is the data dictionary schema that powers the account structure system. Is there a simple way to find the account structure for a given main account?


Solution

  • If you already know the Main Account that you want to get the account structure for, you can use the DimensionHierarchy::getAccountStructure() method, which returns a single account structure ID for that Main Account. From there, you can visit each level in the DimensionHierarchyLevel table for that account structure ID, and extract whatever data you require about the account structure from the DimensionAttribute table.

    DimensionHierarchy hierarchy;
    DimensionHierarchyLevel hierarchyLevel;
    DimensionHierarchyId hId;
    int i = 1; // Set to 0 if you also want to get the root MainAccount level
    
    hId = DimensionHierarchy::getAccountStructure(MainAccount::findByMainAccountId("1234").RecId);
    
    do
    {
        i++;
        hierarchyLevel = DimensionHierarchyLevel::findByDimensionHierarchyAndLevel(hId, i);
        if (hierarchyLevel)
        {
            info(DimensionAttribute::find(hierarchyLevel.DimensionAttribute).Name);
        }
    }
    while (hierarchyLevel);