The following code will (hopefully) keep generating valid LedgerDimension values, even after account structures or advanced rule structures are modified.
In the example below, there is an account structure with MainAccount-BusinessUnit-Department segments and an advanced rule with Project segment.
In the example below, there is an account structure with MainAccount-BusinessUnit-Department segments and an advanced rule with Project segment.
class TestLedgerDim { public static DimensionDynamicAccount generateLedgerDimension( MainAccountNum _mainAccountId, str _departmentId, str _businessUnit, str _projectId) { DimensionAttributeValueSetStorage dimensionAttributeValueSetStorage = new DimensionAttributeValueSetStorage(); void addDimensionAttributeValue( DimensionAttribute _dimensionAttribute, str _dimValueStr) { DimensionAttributeValue dimensionAttributeValue; if (_dimValueStr != '') { dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValueNoError( _dimensionAttribute, _dimValueStr); } if (dimensionAttributeValue.RecId != 0) { dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue); } } DimensionAttribute dimensionAttribute; while select dimensionAttribute where dimensionAttribute.ViewName == tableStr(DimAttributeOMDepartment) || dimensionAttribute.ViewName == tableStr(DimAttributeOMBusinessUnit) || dimensionAttribute.ViewName == tableStr(DimAttributeProjTable) { switch (dimensionAttribute.ViewName) { case tableStr(DimAttributeOMDepartment): addDimensionAttributeValue(dimensionAttribute, _departmentId); break; case tableStr(DimAttributeOMBusinessUnit): addDimensionAttributeValue(dimensionAttribute, _businessUnit); break; case tableStr(DimAttributeProjTable): addDimensionAttributeValue(dimensionAttribute, _projectId); break; } } RecId defaultDimensionRecId = dimensionAttributeValueSetStorage.save(); return LedgerDimensionFacade::serviceCreateLedgerDimension( LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId( MainAccount::findByMainAccountId(_mainAccountId).RecId), defaultDimensionRecId); } /// <summary> /// Runs the class with the specified arguments. /// </summary> /// <param name = "_args">The specified arguments.</param> public static void main(Args _args) { // Prints 110110-001-022-000002 info( DimensionAttributeValueCombination::find( TestLedgerDim::generateLedgerDimension( '110110', '022', '001', '000002')).DisplayValue); } }