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 :
Finance | Project Operations, Human Resources, ...
Answered

Find or create ledger dimension

(0) ShareShare
ReportReport
Posted on by 1,213

Hi All!

I have a requirement for which if some segments of a default dimensions are changed on the line level (for the first line only), I have to copy those segments to the offset account of the header.

When I looked at the LedgerDimensionFacade::serviceCreateLedgerDimension method, it seemed like a possible solution, because,

  • I will extract the main account (MainAccount::findByLedgerDimension) and
  • the default dimension (LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension) from the original OffsetLedgerDimension,
  • copy the segments changed and form a new or find an existing ledger dimension recid.

To test my understanding of the methods

  • I passed one offset ledger dimension (let's say VarA),
  • extracted the main account (VarB) and
  • the default dimension (VarC).
  • then I passed the values of VarB and VarC to the method LedgerDimensionFacade::serviceCreateLedgerDimension

But instead of returning the value equal to VarA, the method returns some other recid.

If you have any information on what am I doing wrong, could you please share it with me...

With best regards,

Abhinay

I have the same question (0)
  • Verified answer
    Abhinay Tharwal Profile Picture
    1,213 on at
    RE: Find of create ledger dimension

    Hi All,

    Thank you for your replies, but the final solution was a bit more than that.

    This is the final solution:

    public static container getLedgerDimDefaultDimMerge(recId _ledgerDimesnions, DimensionDefault _defaultDimensionRecId)
        {
            DimensionHierarchy hierarchy;
            DimensionHierarchyLevel hierarchyLevel;
            DimensionHierarchyId hId;        
            hId = DimensionHierarchy::getAccountStructure(MainAccount::findByMainAccountId(MainAccount::findByLedgerDimension(_ledgerDimesnions).MainAccountId).RecId);
            DimensionStorage dimensionStorage = DimensionStorage::findById(_ledgerDimesnions);
            container conLedgerDim, conDefaultDim;        
            
            container segments = conNull();
            // Get segments
            int segmentCount = dimensionStorage.segmentCountForHierarchy(1);
            String255 segmentName, MainAccount, segmentValue;
            for (int segmentIndex = 1; segmentIndex <= segmentCount; segmentIndex  )
            {
                // Get segment
                DimensionStorageSegment segment = dimensionStorage.getSegmentForHierarchy(1, segmentIndex);
                
                // Get segment name
                hierarchyLevel = DimensionHierarchyLevel::findByDimensionHierarchyAndLevel(hId, segmentIndex);
                segmentName = DimensionAttribute::find(hierarchyLevel.DimensionAttribute).Name;
                
                // Add segment value
                segmentValue = segment.parmDisplayValue();
                if (segmentIndex == 1)
                {
                    MainAccount = segmentValue;
                }
                conLedgerDim  = segmentName;
                conLedgerDim  = segment.parmDisplayValue();            
            }
            
            for (int i = 1; i <= conLen(conLedgerDim); i  = 2)
            {
                Name name = conPeek(conLedgerDim, i);
                DefaultDimensionView defaultDimensionView;
                select defaultDimensionView
                    where defaultDimensionView.DefaultDimension == _defaultDimensionRecId
                            && defaultDimensionView.Name == conPeek(conLedgerDim, i);
                conDefaultDim  = defaultDimensionView.DisplayValue != strMin() ? defaultDimensionView.DisplayValue : conPeek(conLedgerDim, i   1);
            }
            return conDefaultDim;
        }

    and passed the container returned in the above given code to:

    static int64 getLedgerDimFromMainAccAndDefaultDim(MainAccountNum _mainAccountNum, container _conValue)
        {
            container   _conData;
            int hierarchyCount;
            int hierarchyIdx;
            RecId                   dimAttId_MainAccount;
            LedgerRecId            ledgerRecId;
            MainAccount mainAccount;
            RefRecId recordvalue;
            DimensionAttribute     dimensionAttribute;
            DimensionAttributeValue dimensionAttributeValue;
            DimensionSetSegmentName DimensionSet;
            DimensionStorage         dimStorage;
            LedgerAccountContract LedgerAccountContract = new LedgerAccountContract();
            DimensionAttributeValueContract  ValueContract;
            List   valueContracts = new List(Types::Class);
            dimensionAttributeValueCombination dimensionAttributeValueCombination;
            _conData = _conValue;
            mainAccount = MainAccount::findByMainAccountId(_mainAccountNum);
            recordvalue = DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
            hierarchyCount = DimensionHierarchy::getLevelCount(recordvalue);
            DimensionSet = DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);
            for(hierarchyIdx = 1; hierarchyIdx<=hierarchyCount; hierarchyIdx  )
            {
                if(hierarchyIdx == 1)
                    continue;
                dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx], false, "en-in");
                if(dimensionAttribute)
                {
                    dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,conPeek(_conData, hierarchyIdx));
                    if(dimensionAttributeValue)
                    {
                        ValueContract = new DimensionAttributeValueContract();
                        ValueContract.parmName(dimensionAttribute.Name) ;
                        ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
                        valueContracts.addEnd(ValueContract);
                    }
                }
            }
            LedgerAccountContract.parmMainAccount(_mainAccountNum);
            LedgerAccountContract.parmValues(valueContracts);
            dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
            dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save());
            ledgerRecId = dimensionAttributeValueCombination.RecId;
            return ledgerRecId;
        }

    Of-course, I am still testing the code for different scenarios.

    With best regards,

    Abhinay

  • Deepak Agarwal Profile Picture
    8,585 on at
    RE: Find of create ledger dimension

    Hi,

    Yes, this method will return a rec id, if you look into the code it's returning a rec id of DimensionAttributeValueCombination. If you want to store the output on a table then you can store the RecId directly but in case you want to use the dim values further try to play around the DimensionAttributeValueCombination table.

  • Suggested answer
    Sankar Ramalingam Profile Picture
    262 on at
    RE: Find of create ledger dimension

    You could apply the below code

    LedgerDimensionFacade::serviceCreateLedgerDimension(LedgerDefaultAccountHelper::getDefaultAccountFromLedgerDimension(VarB), VarC);

  • huijij Profile Picture
    19,811 on at
    RE: Find of create ledger dimension

    Hi Abhinay,

    Please refer to Sukrut's blog to see if it helps:

    community.dynamics.com/.../msdynd365fo-working-with-dimensions-x-code-part-i

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 823 User Group Leader

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 673 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 491 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans