Sunday, 3 April 2022

Unpack ledger dimension x++ D365

 class FCC_TestDim

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    {

 

        // DimensionAttributeValueCombination stores the combinations of dimension values

// Any tables that uses dimension combinations for main account and dimensions

// Has a reference to this table’s recid

DimensionAttributeValueCombination dimAttrValueComb;

        //GeneralJournalAccountEntry is one such tables that refrences DimensionAttributeValueCombination

        GeneralJournalAccountEntry gjAccEntry;

        // Class Dimension storage is used to store and manipulate the values of combination

        DimensionStorage dimensionStorage;

        // Class DimensionStorageSegment will get specfic segments based on hierarchies

        DimensionStorageSegment segment;

        int segmentCount, segmentIndex;

        int hierarchyCount, hierarchyIndex;

        str segmentName, segmentDescription,segmentValue;

        LedgerJournalTrans ledgerJournalTrans;

        ;

        select ledgerJournalTrans where ledgerJournalTrans.JournalNum == "00457";

 

        dimAttrValueComb = DimensionAttributeValueCombination::find(ledgerJournalTrans.OffsetLedgerDimension);

 

        // Get dimension storage

        dimensionStorage = DimensionStorage::findById(ledgerJournalTrans.OffsetLedgerDimension);

        if (dimensionStorage == null)

        {

            throw error("@SYS83964");

        }

 

        // Get hierarchy count

        hierarchyCount = dimensionStorage.hierarchyCount();

        //Loop through hierarchies to get individual segments

        for(hierarchyIndex = 1; hierarchyIndex <= hierarchyCount; hierarchyIndex++)

        {

 

            //Get segment count for hierarchy

            segmentCount = dimensionStorage.segmentCountForHierarchy(hierarchyIndex);

 

            //Loop through segments and display required values

            for (segmentIndex = 1; segmentIndex <= segmentCount; segmentIndex++)

            {

                // Get segment

                segment = dimensionStorage.getSegmentForHierarchy(hierarchyIndex, segmentIndex);

 

                // Get the segment information

                if (segment.parmDimensionAttributeValueId() != 0)

                {

                    // Get segment name

                    segmentName = DimensionAttribute::find(DimensionAttributeValue::find(segment.parmDimensionAttributeValueId()).DimensionAttribute).Name;

                    //Get segment value (id of the dimension)

                    segmentValue = segment.parmDisplayValue();

                    segmentDescription = segment.getName();

                    if(segmentName =="BusinessUnit")            //Give the Dimension Name to that Dimension Value

                    {

                        info(strFmt("%1Value -- %2Name",segmentValue,segmentDescription));

                        //return segmentDescription;

                    }

                }

            }

        }

       // return "";

    }


}

Unpack ledger dimension x++ ax2012

 static void dimValues(Args _args)

{

    DimensionAttributeValueCombination  dimAttrValueCombination;

    DimensionStorage                    dimensionStorage;

    DimensionStorageSegment             segment;

    int                                 segmentCount, segmentIndex;

    int                                 hierarchyCount, hierarchyIndex;

    str                                 segmentName, segmentDescription;

    SysDim                              segmentValue;

    DimensionHierarchy                  dimensionHierarchy;

    LedgerDimensionAccount              ledgerDimension;

    ledgerJournalTrans                  ledgerJournalTrans;

    RefRecId                            _recid;


    //select ledgerJournalTrans where ledgerJournalTrans.JournalNum == "00457";//.LedgerDimension !=0;


    ledgerDimension = _recId;//ledgerJournalTrans.OffsetLedgerDimension;

    dimAttrValueCombination = DimensionAttributeValueCombination::find(ledgerDimension);


    dimensionStorage = DimensionStorage::findById(ledgerDimension,true);


    dimensionHierarchy = DimensionStorage::getAccountStructureFromLedgerDimension(ledgerDimension);

    hierarchyCount = dimensionStorage.hierarchyCount();

    for(hierarchyIndex = 1; hierarchyIndex <= hierarchyCount; hierarchyIndex++)

    {

        if( dimensionHierarchy.RecId == DimensionHierarchy::find(dimensionStorage.getHierarchyId(hierarchyIndex)).RecId)

        {

            segmentCount = dimensionStorage.segmentCountForHierarchy(hierarchyIndex);

            for (segmentIndex = 1; segmentIndex <= segmentCount; segmentIndex++)

            {

                segment = dimensionStorage.getSegmentForHierarchy(hierarchyIndex, segmentIndex);

                if (segment.parmDimensionAttributeValueId() != 0)

                {

                    segmentName = DimensionAttribute::find(DimensionAttributeValue::find(segment.parmDimensionAttributeValueId()).DimensionAttribute).Name;

                    segmentValue        = segment.parmDisplayValue();

                    segmentDescription  = segment.getName();

                    

                    if(segmentName == "BusinessUnit")

                    {

                        info(strFmt("%1--%2--%3", segmentName, segmentValue, segmentDescription));

                    }

                }

            }

        }

    }

}

Financial dimension unpack & create x++

 class TEST_DefaultDimensions

{

    public DimensionDefault createDimension(container _conAttr, container _conValue)

    {

        DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();

        DimensionDefault                    result;

        int                                 i;

        DimensionAttribute                  dimensionAttribute;

        DimensionAttributeValue             dimensionAttributeValue;

        container                           conAttr     = _conAttr;

        container                           conValue    = _conValue;

        str                                 dimValue;


        for (i = 1; i <= conLen(conAttr); i++)

        {

            dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));

            if (dimensionAttribute.RecId == 0)

            {

                continue;

            }

            dimValue = conPeek(conValue,i);

            if (dimValue != "")

            {

                dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);

                valueSetStorage.addItem(dimensionAttributeValue);

            }

        }

        result = valueSetStorage.save();

        return result;

    }


    public DimensionDefault unpackDimension(CustAccount _custAccount)

    {

        DimensionAttributeValueSetStorage           dimStorage;

        str                                         segmentName, segmentValue, segmentDescription;

        boolean                                     isNullforSales;

        int                                         hierarchyCount, hierarchyIndex;

        DefaultDimensionView                        defaultDimensionView;

        RecId                                       defaultDimension;

        int                                         i;

        CustTable                                   custTable = CustTable::find(_custAccount);

        container                                   conAttribute;

        container                                   conValues;

        DimensionDefault                            result;

    

        defaultDimension    = custTable.DefaultDimension;

        dimStorage          = DimensionAttributeValueSetStorage::find(defaultDimension);

        for (i= 1 ; i<= dimStorage.elements() ; i++)

        {

            segmentName         = DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name;

            segmentValue        = dimStorage.getDisplayValueByIndex(i);

            isNullforSales      = DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).TEST_IsNULLForSales;

            if(segmentName)

            {

                select defaultDimensionView

                where defaultDimensionView.DefaultDimension == defaultDimension

                && defaultDimensionView.Name == segmentName;

    

                segmentDescription = defaultDimensionView.dimensionDiscription();

            }

    

            conAttribute = conIns(conAttribute,1, segmentName);

            if(isNullforSales == true)

            {

                segmentValue = "";

            }

            conValues    = conIns(conValues,1, segmentValue);

        }

        ttsbegin;

        result = this.createDimension(conAttribute, conValues);

        ttscommit;

        return result;

    }


}