Create default dimension

The default dimension hierarchy has it's threshold to understand and work with. Under are two methods I use often and are quite useful in their simplicity.


In advance, sorry for the horrible formatting and indentation of the code.

/// <summary>
/// Creates default dimension for a set of dimension names and values
/// </summary>
/// <param name = "_dimensionNames">Container of dimension names</param>
/// <param name = "_dimensionValues">Container of dimension values</param>
/// <returns>LedgerDefaultDimensionValueSet</returns>
public static LedgerDefaultDimensionValueSet getDefaultDimension(
        container _dimensionNames, container _dimensionValues)
{
    DimensionNameValueListContract      dimensionNameValueListContract = new                                      DimensionNameValueListContract();
    DimensionStorageResult dimensionStorageResult;        
    List dimensionValueList = new List(Types::Class);
    LedgerDefaultDimensionValueSet  ret;       
    DimensionAttributeValueContract dimensionAttributeValueContract;

        if(conLen(_dimensionNames) != conLen(_dimensionValues) || conLen(_dimensionNames) == 0)
        {
            return checkFailed(strFmt("Dimension names and values containers length inconsistent, cannot create default dimension"));
        }
        for(int i= 1; i<= conLen(_dimensionNames); i++)
        {
            str dimensionName   = conPeek(_dimensionNames,  i);
            str dimensionValue  = conPeek(_dimensionValues, i);                                    
         
            dimensionAttributeValueContract = DimensionAttributeValueContract::construct(dimensionName, dimensionValue);                
            dimensionValueList.addEnd(dimensionAttributeValueContract);
        }
        dimensionNameValueListContract.parmValues(dimensionValueList);
        dimensionStorageResult = DimensionNameValueListServiceProvider::newForDimensionNameValueListContract(dimensionNameValueListContract).resolve();                                 
        ret = dimensionStorageResult.parmSavedRecId();
        return ret;
    }

/// <summary>
/// Produces a new default dimension based on other, removing values specified in container
/// </summary>
/// <param name = "_defaultDimensionFrom">based on</param>
/// <param name = "_dimensionNamesToblank">container of dimension names in which to blank</param>
/// <returns>LedgerDefaultDimensionValueSet</returns>
public static LedgerDefaultDimensionValueSet getDefaultDimensionRemoveValueFromDefaultDimension(LedgerDefaultDimensionValueSet _defaultDimensionFrom, container _dimensionNamesToblank)
{
       DimensionNameValueListContract      dimensionNameValueListContract = new                 DimensionNameValueListContract();
      DimensionStorageResult              dimensionStorageResult;
      DimensionAttributeValueSetStorage   dimStorage;
      LedgerDefaultDimensionValueSet      ret;
      List                                dimensionValueList = new List(Types::Class);
       
      DimensionAttributeValueContract dimensionAttributeValueContract;
       // DimensionDefault is a RecId that combines all Dimension Values
        dimStorage = DimensionAttributeValueSetStorage::find(_defaultDimensionFrom);
        for(int i= 1; i<= dimStorage.elements(); i++)
        {                        
            str dimensionName   = DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name;
            str dimensionValue  = dimStorage.getDisplayValueByIndex(i);
                        
            //only add if not in container to blank            
            if(confind(_dimensionNamesToblank, dimensionName) == 0)
            {
                dimensionAttributeValueContract = DimensionAttributeValueContract::construct(dimensionName, dimensionValue);                
                
                dimensionValueList.addEnd(dimensionAttributeValueContract);
            }                        
        }
        dimensionNameValueListContract.parmValues(dimensionValueList);
        dimensionStorageResult = DimensionNameValueListServiceProvider::newForDimensionNameValueListContract(dimensionNameValueListContract).resolve();
                                 
        ret = dimensionStorageResult.parmSavedRecId();
        return ret; 
        
    }

Comments

Popular posts from this blog

Call a simple Logic app from X++

SysOperationFramework with use of Query

Retail Attributes on sales lines