Create offset ledger dimension using string value in D365FO X++:
Str offsetAccountValue = XXXX-XXXXXX-XXXX.XXXX-XXXX-XXXX-XXXXXX-XXXXXX
public RefRecId createOffsetLedgerDimension(str _offsetAccountVal)
{
LedgerAccountContract ledgerAccountContract, ledgerAccountContractLoc;
List valueContracts, valueContractsLoc;
DimensionHierarchy dimHierarchy;
int dimCounterLoc;
DimensionAttribute dimAttributeLoc;
str dimAttValueLoc;
DimensionAttributeValue dimAttributeVal, dimAttributeValLoc;
DimensionAttributeValueContract dimAttributeValueContract, dimAttributeValueContractLoc;
MainAccountNum mainAccountId, mainAccountIdLoc;
DimensionStorage dimStorage, dimStorageLoc;
valueContracts = new List(Types::Class);
valueContractsLoc = new List(Types::Class);
ledgerAccountContract = new LedgerAccountContract();
ledgerAccountContractLoc = new LedgerAccountContract();
{
LedgerAccountContract ledgerAccountContract, ledgerAccountContractLoc;
List valueContracts, valueContractsLoc;
DimensionHierarchy dimHierarchy;
int dimCounterLoc;
DimensionAttribute dimAttributeLoc;
str dimAttValueLoc;
DimensionAttributeValue dimAttributeVal, dimAttributeValLoc;
DimensionAttributeValueContract dimAttributeValueContract, dimAttributeValueContractLoc;
MainAccountNum mainAccountId, mainAccountIdLoc;
DimensionStorage dimStorage, dimStorageLoc;
valueContracts = new List(Types::Class);
valueContractsLoc = new List(Types::Class);
ledgerAccountContract = new LedgerAccountContract();
ledgerAccountContractLoc = new LedgerAccountContract();
select firstonly * from dimHierarchy
where dimHierarchy.StructureType == DimensionHierarchyType::DataEntityLedgerDimensionFormat
&& dimHierarchy.IsDraft == NoYes::No;
if(offsetAccountVal)
{
for(int counterLoc = 0; counterLoc <= strLen(offsetAccountVal); counterLoc++)
{
dimCounterLoc++;
dimAttributeLoc = DimensionAttribute::find(DimensionHierarchyLevel::findByDimensionHierarchyAndLevel(dimHierarchy.RecId, dimCounterLoc).DimensionAttribute);
if(strFind(offsetAccountVal, "-", counterLoc, strLen(offsetAccountVal)) > 0)
{
dimAttValueLoc = subStr(offsetAccountVal, counterLoc, (strFind(offsetAccountVal, "-", counterLoc, strLen(offsetAccountVal)) - counterLoc));
if(strFind(dimAttValueLoc, "-",0, strLen(dimAttValueLoc)) > 0)
{
dimAttValueLoc = subStr(dimAttValueLoc, 0, (strFind(dimAttValueLoc, "-",0, strLen(dimAttValueLoc)) - 1));
}
counterLoc = strFind(offsetAccountVal, "-", counterLoc, strLen(offsetAccountVal));
}
else
{
dimAttValueLoc = subStr(offsetAccountVal, counterLoc, strLen(offsetAccountVal));
counterLoc = strLen(offsetAccountVal);
}
dimAttributeValLoc = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttributeLoc, dimAttValueLoc);
if(dimAttributeLoc.Name == "MainAccount")
{
if(!MainAccount::findByMainAccountId(dimAttValueLoc).RecId)
{
throw Error(strFmt("Invalid value of %1",dimAttributeLoc.Name));
}
mainAccountIdLoc = dimAttValueLoc;
continue;
}
if(dimAttributeValLoc.RecId)
{
dimAttributeValueContractLoc = new DimensionAttributeValueContract();
dimAttributeValueContractLoc.parmName(dimAttributeLoc.Name);
dimAttributeValueContractLoc.parmValue(dimAttributeValLoc.CachedDisplayValue);
valueContractsLoc.addEnd(dimAttributeValueContractLoc);
}
else
{
throw Error(strFmt("Invalid value of %1",dimAttributeLoc.Name));
}
}
ledgerAccountContractLoc.parmMainAccount(mainAccountIdLoc);
ledgerAccountContractLoc.parmValues(valueContractsLoc);
dimStorageLoc = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(ledgerAccountContractLoc);
return DimensionAttributeValueCombination::find(dimStorageLoc.save()).RecId;
}
return 0;
}
You can get format combination from the below line. Currently it's for ledger dimension, you can also get the same for other dimension by changing the type of enum (DimensionHierarchyType).
select firstonly * from dimHierarchy
where dimHierarchy.StructureType == DimensionHierarchyType::DataEntityLedgerDimensionFormat
&& dimHierarchy.IsDraft == NoYes::No;
where dimHierarchy.StructureType == DimensionHierarchyType::DataEntityLedgerDimensionFormat
&& dimHierarchy.IsDraft == NoYes::No;
No comments:
Post a Comment