Tuesday, 30 January 2018

Resolve ledger dimension through X++ [D365FO]

A bit of code to show how to resolve ledger dimensions. There are various codes out there but I thought I would write it in an easy way to understand. It is hard code but I did that for illustration purposes.

   
   public static void getLedgerDimension()  
   {  
     DimensionAttribute     dimensionAttribute;  
     DimensionAttributeValue   dimensionAttributeValue;  
     DimensionSetSegmentName   dimensionSet;  
     DimensionStorage      dimStorage;  
   
     LedgerAccountContract ledgerAccountContract = new LedgerAccountContract();  
     ledgerAccountContract.parmValues(new List(Types::Class));  
     ledgerAccountContract.parmAccountStructure('Manufacturing B/S');  
      
     DimensionAttributeValueContract dimensionAttributeValueContract;  
   
     //Main account  
     ledgerAccountContract.parmMainAccount('110180');  
       
     //Dimension 1 - repeat this for all other dimensions  
     dimensionAttributeValueContract = DimensionAttributeValueContract::construct('Department', '022');  
     ledgerAccountContract.parmValues().addEnd(dimensionAttributeValueContract);      
       
     //resolve the dimension  
     LedgerDimensionCombinationServiceProvider dimensionServiceProvider = LedgerDimensionCombinationServiceProvider::newForLedgerAccountContract(ledgerAccountContract);  
     DimensionStorageResult dimensionStorageResult = dimensionServiceProvider.resolve();  
       
     if (dimensionStorageResult.parmInvalidValue())  
     {  
       error("Invalid dimension");  
     }  
   
     info(strFmt("Ledger dimension RecId: %1", dimensionStorageResult.parmSavedRecId()));  
   }  


There are a few other ways that do the same. Another example is using this method.

LedgerAccountDimensionResolver::newResolver

Use find reference to get some examples. However I prefer this method above.

No comments: