Hi Folks,
Often we need to create Journals via X++ code as part of customization or integration and dimension is always the tricky part of this. How to validate the dimension combination value as per rules set in the system.
I am sharing a code sample to do the same, check this code and you may need to replace some dimension names or add/remove dimensions as per your requirement. The below code sample will validate
1. Main account
2. Individual dimension value
3. Deactivated dimension values
4. Dimension combination
| MainAccount mainAccount; | |
| DimensionAttributeValue dimensionAttributeValue; | |
| DimensionAttribute dimensionAttribute; | |
| container dimCon = str2con(myTable.AccountDisplayValue, "-"); // This will break dimension combinaiton into different fields | |
| LedgerAccountValidationContract validationContract; | |
| LedgerAccountContract accountContract; | |
| DimensionAttributeValueContract valueContract; | |
| FinancialDimensionValidationService service; | |
| List listValueContract = new List(Types::Class); | |
| // division | |
| if(conPeek(dimCon, 2) != "") // Use this code to pick dimension as per appear in combination, eg. Division is the dimension which appears at position 2 in dimension value | |
| { | |
| valueContract = new DimensionAttributeValueContract(); | |
| valueContract.parmName("Division"); //Dimension name | |
| valueContract.parmValue(conPeek(dimCon, 2)); //Position within dimension value | |
| listValueContract.addEnd(valueContract); | |
| } | |
| // Cost centre | |
| if(conPeek(dimCon, 3) != "") | |
| { | |
| valueContract = new DimensionAttributeValueContract(); | |
| valueContract.parmName("CostCentre"); | |
| valueContract.parmValue(conPeek(dimCon, 3)); | |
| listValueContract.addEnd(valueContract); | |
| } | |
| // Business unit | |
| if(conPeek(dimCon, 4) != "") | |
| { | |
| valueContract = new DimensionAttributeValueContract(); | |
| valueContract.parmName("BusinessUnit"); | |
| valueContract.parmValue(conPeek(dimCon, 4)); | |
| listValueContract.addEnd(valueContract); | |
| } | |
| accountContract = new LedgerAccountContract(); | |
| accountContract.parmMainAccount(conPeek(dimCon, 1)); | |
| accountContract.parmValues(listValueContract); | |
| validationContract = new LedgerAccountValidationContract(); | |
| validationContract.parmLedgerAccount(accountContract); | |
| validationContract.parmValidDate(systemDateGet()); | |
| service = new FinancialDimensionValidationService(); | |
| if (!service.validateLedgerAccount(validationContract)) | |
| { | |
| ret = false; | |
| erroMessages = strFmt("Dimension combination is in valid"); | |
| } |