web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / AXology / Account structures created ...

Account structures created using X++

Søren Rasmussen - Bredana Profile Picture Søren Rasmussen - B... 817

This is probably one of the cases that goes in the category of oddities. Working on a customer case we had to enable creation of account structures through code.

Looking at how it is done in the standard form did not give us that easy solution we had hoped for. Everything was nicely packaged into some .net classes with assumptions we at this point could not comply with. So we started looking at how to make it by ourselves. I realise that this might not be best practice and if anyone can point me towards a more elegant way please do so. Until then this post offers a quick way to create an account structure using X++.

I have packed it into a class that goes like this:

class DemoCreateAccountStructure
{
    DimensionHierarchy dimHierarchy;
    DimensionConstraintTree dimConstraintTree;
    int dimLevelNum;
}

private void createHierarchy(HierarchyName _name, Description _description)
{
    dimHierarchy.initValue();
    dimHierarchy.Description = _description;
    dimHierarchy.DraftDescription = dimHierarchy.Description + " - DRAFT";
    dimHierarchy.Name = _name;
    dimHierarchy.IsDraft = true;
    dimHierarchy.DraftName = dimHierarchy.Name + "_DRAFT";
    dimHierarchy.StructureType = DimensionHierarchyType::AccountStructure;
    dimHierarchy.insert();

    dimConstraintTree.DimensionHierarchy = dimHierarchy.RecId;
    dimConstraintTree.insert();
}

private RefRecId addLevelToTree(DimensionAttributeRecId _attribute, RefRecid _parentLevelId, Name _name, Description _description, DimensionOrdinal _ordinal, DimensionFromValue _from, DimensionToValue _to, boolean _additionalCriteria = false)
{
    DimensionConstraintNodeCriteria dimConstraintNodeCriteria;
    DimensionConstraintNode dimConstraintNode;
    DimensionHierarchyLevel dimHierarchyLevel;

    if (! _additionalCriteria)
    {
        // Insert level in tree
        // ---------------------------------
        dimLevelNum++;
        dimHierarchyLevel.clear();
        dimHierarchyLevel.DimensionHierarchy = dimHierarchy.RecId;
        dimHierarchyLevel.Level = dimLevelNum;
        dimHierarchyLevel.DimensionAttribute = _attribute;
        dimHierarchyLevel.insert();
    }

    // Add range node to level
    // ------------------------
    dimConstraintNode.clear();
    dimConstraintNode.Description = _description;
    dimConstraintNode.DimensionConstraintTree = dimConstraintTree.RecId;
    dimConstraintNode.DimensionHierarchyLevel = dimHierarchyLevel.RecId;
    dimConstraintNode.IsOptional = false;
    dimConstraintNode.Name = _name;
    dimConstraintNode.Ordinal = _ordinal;
    dimConstraintNode.ParentConstraintNode = _parentLevelId;
    dimConstraintNode.insert();

    // Add criteria to node
    dimConstraintNodeCriteria.clear();
    dimConstraintNodeCriteria.Ordinal = 1;
    dimConstraintNodeCriteria.DimensionConstraintNode = dimConstraintNode.RecId;

    if (_from || _to)
    {
        dimConstraintNodeCriteria.IsFromOpen = false;
        dimConstraintNodeCriteria.IsToOpen = false;
        dimConstraintNodeCriteria.RangeFrom = _from;
        dimConstraintNodeCriteria.RangeTo = _to;
    }
    else
    {
        dimConstraintNodeCriteria.WildCardString = '%';
        dimConstraintNodeCriteria.IsFromOpen = true;
        dimConstraintNodeCriteria.IsToOpen = true;
    }

    dimConstraintNodeCriteria.insert();
    return dimConstraintNode.RecId;
}

So what is happening here? Well, we use the createHierarchy method to create the foundation of the account structure –  being the hierarchy specifying names, descriptions, type and status – and the tree/root of the structure we are about to add structure elements in to.

To add the elements/nodes to the tree we use the method addLevelToTree. The method creates the structure level, a constraint node and the nested criteria. It needs to know the type, parent node, name, ordinal number and ranges. The last boolean is a matter of it just adds ranges to the previously created level.

To execute the above here is an example. It create a structure with the main account being “123123″, a cost center of your own choice and the department with three options being IT, adm or sales.

RefRecId mainAccountLevel;
RefRecId costCenterLevel;

ttsBegin;

this.createHierarchy('MyHierarchy', 'My hierarchy');

mainAccountLevel = this.addLevelToTree(5637144583, 0, 'MainAccount', 'Main account', 1, '123123', '123123'); // Main account
costCenterLevel = this.addLevelToTree(22565421197, MainAccountLevel, 'CostCenter', 'Cost center', 1, '', ''); // Cost center

this.addLevelToTree(22565421196, costCenterLevel, 'Department', 'Department', 1, 'Adm', 'Adm'); // Department
this.addLevelToTree(22565421196, costCenterLevel, 'Department', 'Department', 2, 'IT', 'IT', true); // Department
this.addLevelToTree(22565421196, costCenterLevel, 'Department', 'Department', 3, 'Sales', 'Sales', true); // Department

ttsCommit;

 

Naming and getting the right DimensionAttribute record ids for Main account, Cost center and Department is up to you.

Just to clarify (and repeat myself), this is probably not best practice and is to be used with caution and only as an example of how to create the records in the data structure. It is not the complete guide to the subject and the code could be optimised in several ways.

And if you know a better way using standard classes please point me in a better direction. :)

 



This was originally posted here.

Comments

*This post is locked for comments