How to merge custom default dimension to Ledger Dimension







static LedgerDimensionAccount replaceLedgerCustomDim(LedgerDimensionAccount _sourceDimension, Name _dimAttributeName, DimensionValue _dimensionValue)
{
//_dimAttributeName >> Site
//_dimensionValue >> "00"
//>> replace "01" to your ledger dimension Site
    #LedgerSHA1Hash
    DimensionSHA1Hash               hash; //To store the calculated hash for DimensionAttributeValueSet
    HashKey                         valueKeyHashArray[]; //To store the has key of dimension in question
    Map                             dimAttrIdx, dimAttrRecId; //to store the dimension index and backing entity type
    DimensionAttributeSetItem       dimAttrSetItem; // Contains the number of dimensions active for a account structure ledger
    DimensionAttribute              dimAttr; // Contains the financial dimensions records
    DimensionFinancialTag           dimensionFinancialTag ; //Backing entity view for Custome type dimension //>> you can change other view contain existing Dimension
    DimensionAttributeValue         dimAttrValue; // Contains used financial dimension values
    DimensionAttributeValueSet      dimAttrValueSet; //Contains default dimension records
    DimensionAttributeValueSetItem  dimAttrValueSetItem; //Contains individual records for default dimensions
    LedgerDimensionAccount          sourceDimension = _sourceDimension, targetDimension; //Record Id DimensionAttributeValueCombination table in which attribute value is to be replaced
    DimensionDefault                sourceDefDimension, targetDefDimension; //To hold the default dimension combination from a Ledger dimension
    LedgerDimensionDefaultAccount   defaultLedgerAccount;
    DimensionEnumeration            dimensionSetId; //Record id for table that contains active dimensions for current ledger

    int                             dimAttrCount, i;
    int                             backEntityType; //Stores the backing entity type for Custom type dimension
    ;

    //The Custom backing entity will be the view DimAttributeHcmWorker
    backEntityType = tableNum(DimensionFinancialTag);

    //Initialize the map to store the backing entity types
    dimAttrIdx = new Map(Types::Integer, Types::Integer);
    dimAttrRecId = new Map(Types::Integer, Types::Int64);

    //Get the record Id (dimension set id) for current ledger to find active dimensions
    dimensionSetId = DimensionCache::getDimensionAttributeSetForLedger();

    //Find all the active dimensions for current ledger except main account and store there
    //backing entity type in the map
    while select * from dimAttr
            order by Name
            where dimAttr.Type != DimensionAttributeType::MainAccount
        join RecId from dimAttrSetItem
            where dimAttrSetItem.DimensionAttribute == dimAttr.RecId &&
                dimAttrSetItem.DimensionAttributeSet == dimensionSetId
    {
        dimAttrCount++;
        dimAttrIdx.insert(dimAttr.BackingEntityType, dimAttrCount);
    }

    //initialize hash key array to null
    for (i = 1; i<= dimAttrCount; i++)
    {
        valueKeyHashArray[i] = emptyGuid();
    }

    // Get the default dimensions from Ledger dimensions
    sourceDefDimension      = DimensionStorage::getDefaultDimensionFromLedgerDimension(sourceDimension);

    //Get the default account from Ledger dimensions
    defaultLedgerAccount    = DimensionStorage::getLedgerDefaultAccountFromLedgerDim(sourceDimension);

    //Find the Dimension attribute record for the dimension to work on
    dimAttr.clear();
    select firstOnly dimAttr
        where dimAttr.BackingEntityType == backEntityType &&
                dimAttr.Type            != DimensionAttributeType::DynamicAccount &&
                dimAttr.Name            == _dimAttributeName;

    //Get the backing entity type for the dimension value to process
    select firstOnly dimensionFinancialTag
        where dimensionFinancialTag.Value == _dimensionValue &&
                     dimensionFinancialTag.FinancialTagCategory == dimAttr.financialTagCategory();
    if (dimensionFinancialTag)
    {
        //Find the required Dimension Attribute Value record
        //Create if necessary
        dimAttrValue = DimensionAttributeValue::findByDimensionAttributeAndEntityInst(dimAttr.RecId, dimensionFinancialTag.RecId, false, true);

        //Store the required combination hash keys
        valueKeyHashArray[dimAttrIdx.lookup(backEntityType)] = dimAttrValue.HashKey;

        //Calculate the hash for the current values
        hash = DimensionAttributeValueSetStorage::getHashFromArray(valueKeyHashArray, dimAttrCount);

        //Null hash indicates no values exist, which may occur if the user entered an invalid value for one dimension attribute
        if (hash == conNull())
        {
            throw error(strFmt("Wrong value for %1 Dimension",_dimAttributeName));
        }

        // Search for existing value set
        dimAttrValueSet = DimensionAttributeValueSet::findByHash(hash);

        // This value set does not exist, so it must be persisted
        if (!dimAttrValueSet)
        {
            ttsbegin;

            // Insert the value set with appropriate hash
            dimAttrValueSet.Hash = hash;
            dimAttrValueSet.insert();

            //Insert Custom dimension set item
            dimAttrValueSetItem.clear();
            dimAttrValueSetItem.DimensionAttributeValueSet = dimAttrValueSet.RecId;
            dimAttrValueSetItem.DimensionAttributeValue = dimAttrValue.RecId;
            dimAttrValueSetItem.DisplayValue = dimensionFinancialTag.Value;
            dimAttrValueSetItem.insert();

            ttscommit;
        }

        //Replace the value in default dimension
        targetDefDimension = DimensionDefaultingService::serviceReplaceAttributeValue(sourceDefDimension, dimAttrValueSet.RecId, dimAttr.RecId);

        //Combine the target default dimension, default ledger account to get the resultant ledger dimension
        targetDimension = DimensionDefaultingService::serviceCreateLedgerDimension(defaultLedgerAccount, targetDefDimension);

        //info(strFmt("Before: %1", DimensionAttributeValueCombination::find(sourceDimension).DisplayValue));
        //info(strFmt("After: %1", DimensionAttributeValueCombination::find(targetDimension).DisplayValue));
    }
    return targetDimension;
}