2013年6月14日星期五

How to remove a Financial Dimension Value in AX2012

Here is a job that can help you remove or blank out a financial dimension.
Note the Dimensions before executing this job


Job
static void sgxRemoveWorkerDimension(Args _args)
{
/*
* We will Remove a worker dimension in this example
*/
CustTable custTable = CustTable::find(‘CUS-00004′); //Customer Record containing Financial Dimension
DimensionSHA1Hash hash; //To store the calculated hash for DimensionAttributeValueSet
DimensionAttribute dimAttr; // Contains the financial dimensions records
DimensionAttributeValue dimAttrValue; // Contains used financial dimension values
DimensionAttributeValueSet dimAttrValueSet; //Contains default dimension records
DimensionAttributeValueSetItem dimAttrValueSetItem; //Contains individual records for default dimensions
DimensionAttributeSetItem dimAttrSetItem; // Contains the number of dimensions active for a account structure ledger

HashKey valueKeyHashArray[]; //To store the has key of dimension in question
Map dimAttrRecId, dimAttrStr; //To store the dimension attribute recid and dimension attribute value display value
Set dimAttrValueRecId;
SetEnumerator setEnum;
int dimAttrCount, i;


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

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

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

//Get individual dimension attribute records and display values except worker dimension
//Store them in sets
while select DisplayValue, DimensionAttributeValue from dimAttrValueSetItem
where dimAttrValueSetItem.DimensionAttributeValueSet == custTable.DefaultDimension
join DimensionAttribute, HashKey from dimAttrValue
where dimAttrValue.RecId == dimAttrValueSetItem.DimensionAttributeValue
join RecId from dimAttr
where dimAttr.RecId == dimAttrValue.DimensionAttribute
&& dimAttr.BackingEntityType != tableNum(DimAttributeHcmWorker) //As we ignore worker dimension, its hash key remains blank
{
dimAttrValueRecId.add(dimAttrValueSetItem.DimensionAttributeValue);
dimAttrStr.insert(dimAttrValueSetItem.DimensionAttributeValue, dimAttrValueSetItem.DisplayValue);
valueKeyHashArray[dimAttrRecId.lookup(dimAttrValue.DimensionAttribute)] = 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(“Wrong value for Dimensions”);
}

// 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 only specified set items use this
setEnum = dimAttrValueRecId.getEnumerator();

while (setEnum.moveNext())
{
dimAttrValueSetItem.clear();
dimAttrValueSetItem.DimensionAttributeValueSet = dimAttrValueSet.RecId;
dimAttrValueSetItem.DimensionAttributeValue = setEnum.current();
dimAttrValueSetItem.DisplayValue = dimAttrStr.lookup(setEnum.current());
dimAttrValueSetItem.insert();
}
ttscommit;
}

ttsBegin;
custTable.selectForUpdate(true);
custTable.DefaultDimension = dimAttrValueSet.RecId;
custTable.doUpdate();
ttsCommit;
}
Result After Job Run
image

没有评论:

发表评论