X++ Code To Generate Ledger Dimension Based On The Company Accounting Structure.
public static RefRecId generateLedgerDimension(MainAccountNum _mainAccountId,
DimensionValue _costCentre,DimensionValue _division,DimensionValue _location,
DimensionValue _supplier, DimensionValue _products,DimensionValue _customer,
DimensionValue _interCompany)
{
container conData;
int hierarchyCount;
int hierarchyIdx;
LedgerRecId ledgerRecId;
MainAccount mainAccount;
RefRecId recordvalue;
DimensionAttribute dimensionAttribute;
DimensionAttributeValue dimensionAttributeValue;
DimensionSetSegmentName DimensionSet;
DimensionStorage dimStorage;
DimensionAttributeValueContract ValueContract;
LedgerAccountContract LedgerAccountContract;
DimensionAttributeValueCombination dimensionAttributeValueCombination;
List valueContracts;
#define.MainAccount('MainAccount')
#define.Division('Division')
#define.CostCentre('CostCentre')
#define.Location('Location')
#define.Supplier('Supplier')
#define.Products('Products')
#define.Intercompany('Intercompany')
#define.Customer('Customer')
#define.Language('en-gb')
LedgerAccountContract = new LedgerAccountContract();
valueContracts = new List(Types::Class);
conData =[_mainAccountId];
mainAccount = MainAccount::findByMainAccountId(_mainAccountId);
recordvalue = DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
hierarchyCount = DimensionHierarchy::getLevelCount(recordvalue);
DimensionSet = DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);
if (recordvalue)
{
for(hierarchyIdx = 1;hierarchyIdx<=hierarchyCount;hierarchyIdx++)
{
if(hierarchyIdx == 1)
continue;
dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx],false,#Language);
if (dimensionAttribute.Name == #Division)
{
conData += [_division];
}
if (dimensionAttribute.Name == #CostCentre)
{
conData += [_costCentre];
}
if (dimensionAttribute.Name == #Supplier)
{
conData += [_supplier];
}
if (dimensionAttribute.Name == #Location)
{
conData += [_location];
}
if (dimensionAttribute.Name == #Products)
{
conData += [_products];
}
if (dimensionAttribute.Name == #Intercompany)
{
conData += [_interCompany];
}
if (dimensionAttribute.Name == #Customer)
{
conData += [_customer];
}
if(dimensionAttribute)
{
dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,conPeek(conData,hierarchyIdx));
if(dimensionAttributeValue)
{
ValueContract = new DimensionAttributeValueContract();
ValueContract.parmName(dimensionAttribute.Name) ;
ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
valueContracts.addEnd(ValueContract);
}
}
}
LedgerAccountContract.parmMainAccount(_mainAccountId);
LedgerAccountContract.parmValues(valueContracts);
dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save());
ledgerRecId = dimensionAttributeValueCombination.RecId;
}
else
{
warning (strfmt("Ledger dimension Issue for main account %1", _mainAccountId));
}
return ledgerRecId;
}
Thank for such as useful Post, Just need to Know Its also work for advance rule account Structure.
ReplyDeleteThanx for very useful info! I have been searched it for a long time. God bless!
ReplyDelete