Thursday 19 December 2013

How to get ledger dimension values for active account structure in AX 2012

These are the jobs for getting ledger dimension values for active account structure in AX 2012
1*
static void getDimensionCombinationValues(Args _args)
{
    DimensionAttributeLevelValueAllView dimensionAttributeLevelValueAllView;
    DimensionAttribute                  dimensionAttribute;   
    Set                                 dimensionAttributeProcessed;
    LedgerDimensionAccount              _ledgerDimension;
    str                                 segmentName, segmentDescription;
    SysDim                              segmentValue;
     str getDynamicAccountAttributeName(TableNameShort _dimensionAttrViewName)
    {
        #Dimensions

        container cachedResult;
        SysModelElement modelElement;
        SysDictTable sysDictTable;
        DictView dictView;
        Label label;

        Debug::assert(_dimensionAttrViewName like #DimensionEnabledPrefixWithWildcard);

        // Get/cache results of the AOT metadata lookup on the view
        cachedResult = DimensionCache::getValue(DimensionCacheScope::DynamicAccountAttributeName, [_dimensionAttrViewName]);

        if (cachedResult == conNull())
        {
            // Find the matching model element and instantiate the AOT metadata definition of the view
            select firstOnly AxId, Name from modelElement
                where  modelElement.ElementType == UtilElementType::Table
                    && modelElement.Name == _dimensionAttrViewName;

            sysDictTable = new sysDictTable(modelElement.AxId);
            Debug::assert(sysDictTable.isView());

            // Create an instance of the view and get the singular representation of the entity name as a label ID (do not translate)
            dictView = new dictView(modelElement.AxId);

            cachedResult = [dictView.singularLabel()];
            DimensionCache::insertValue(DimensionCacheScope::DynamicAccountAttributeName, [_dimensionAttrViewName], cachedResult);
        }

        label = new label();
        return label.extractString(conPeek(cachedResult, 1));
    }
    _ledgerDimension = 22565435768;
    if (_ledgerDimension)
    {
        dimensionAttributeProcessed = newSet(extendedTypeId2Type(extendedTypeNum(DimensionAttributeRecId)));       

        while select DisplayValue, AttributeValueRecId from dimensionAttributeLevelValueAllView
            order by dimensionAttributeLevelValueAllView.GroupOrdinal, dimensionAttributeLevelValueAllView.ValueOrdinal
            where dimensionAttributeLevelValueAllView.ValueCombinationRecId == _ledgerDimension
            join Name, Type, ViewName, RecId from dimensionAttribute
                where dimensionAttribute.RecId == dimensionAttributeLevelValueAllView.DimensionAttribute 
        {
            if (!dimensionAttributeProcessed.in(dimensionAttribute.RecId))
            {

                if (DimensionAttributeType::DynamicAccount == dimensionAttribute.Type)
                {
                    // Use the singular name of the view backing the multi-typed entity
                    segmentName = getDynamicAccountAttributeName(dimensionAttribute.ViewName);
                }
                else
                {
                    // Use the name of the attribute directly for all other types (main account, custom list, existing list)
                    segmentName = dimensionAttribute.localizedName();
                }

                segmentValue = dimensionAttributeLevelValueAllView.DisplayValue;
                segmentDescription = DimensionAttributeValue::find(
                    dimensionAttributeLevelValueAllView.AttributeValueRecId).getName();

                dimensionAttributeProcessed.add(dimensionAttribute.RecId);               
                info(strFmt("%1: %2, %3", segmentName, segmentValue, segmentDescription));
            }
        }
    }
} 

 2*
static void getActiveDimensionCombinationValues(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  dimAttrValueCombination;   
    // 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;
    SysDim                  segmentValue;
    DimensionHierarchy      dimensionHierarchy;
    LedgerDimensionAccount  ledgerDimension;
    ;
      
    ledgerDimension = 22565435768;
    setPrefix("Dimension values");
    //Fetch the Value combination record
    dimAttrValueCombination = DimensionAttributeValueCombination::find(ledgerDimension);
    setPrefix("Breakup for " + dimAttrValueCombination.DisplayValue);

    // Get dimension storage
    dimensionStorage = DimensionStorage::findById(ledgerDimension,true);
   
    if (dimensionStorage == null)
    {
        throw error("@SYS83964");
    }
    dimensionHierarchy = DimensionStorage::getAccountStructureFromLedgerDimension(ledgerDimension);
    // Get hierarchy count
    hierarchyCount = dimensionStorage.hierarchyCount();
    //Loop through hierarchies to get individual segments
    for(hierarchyIndex = 1; hierarchyIndex <= hierarchyCount; hierarchyIndex++)
    {
        //get segment value for active account structure
        if( dimensionHierarchy.RecId == DimensionHierarchy::find(dimensionStorage.getHierarchyId(hierarchyIndex)).RecId)
        {
            setPrefix(strFmt("Hierarchy: %1", dimensionHierarchy.Name));
            //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();
                    //Get segment value name (Description for dimension)
                    segmentDescription  = segment.getName();
                    info(strFmt("%1: %2, %3", segmentName, segmentValue, segmentDescription));
                }
            }
        }
    }
}

No comments:

Post a Comment