Dynamics Search Engine

Wednesday, June 15, 2016

In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the financial dimension structure along with ledger account or any other account by X++ code?

From this article you may get answer of below questions.
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the financial dimension structure by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the dimension structure along with main account by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the financial dimension structure along with ledger account, bank account, customer account or any other account by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) different ways to get the financial dimension structure by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get financial dimension attribute and its value by X++ code?


Applied on: Dynamics AX 2012 R3 CU8.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience
: AX programmers.
Assumption
: You are familiar with Dynamics AX 2012.





Please see the above image, it’s a financial dimension on sales order, in general such kind of field holds a RecId if you see it at table level. Question is how to get all these dimensions and their values by X++ code?
You may have faced the requirement where you need to bring the dimension structure along with an account. E.g. a ledger account should be flown along with the default or financial dimension structure. Let’s say ledger account number 3235 and above shown financial dimension should be added and final outcome should be like 3235-001-100-047-00001. How to get this ledger account along with structure by x++ code?
There are two ways to get this done.

Option 1:
In below example I need to assign offset ledger dimension to ledgerJournalTrans table field along with financial dimension from SalesTable. When you need to create payment journal and voucher by X++ code, it could be a perfect requirement match for you.

LedgerJournalTrans.OffsetLedgerDimension = DimensionDefaultingService::serviceCreateLedgerDimension(5637189003,SalesTable.DefaultDimension);

Here serviceCreateLedgerDimension() is a method of DimensionDefaultingService class which gives you a RecId with structure combination. To this method I’m passing two parameters, 1st one is a ledger Account coming from Account Receivable parameter which is account number 10140 and 2nd one is coming from SalesTable.DefaultDimension field. This is according to my requirement. In my case I should get the outcome like 10140--001-100-047-00001 as return value from serviceCreateLedgerDimension() method which again nothing a RecId only and on left hand side LedgerJournalTrans.OffsetLedgerDimension field is expecting a RecId.






Option 2:
In below example I need to assign offset ledger dimension to ledgerJournalTrans table field along with financial dimension from SalesTable.
//acctPattern = ['10140','10140',4,'BusinessUnit','001','CostCenter','100','Division','047','Location','00001'];

LedgerJournalTrans.OffsetLedgerDimension = AxdDimensionUtil::getLedgerAccountId(acctPattern);

Here I’m giving a parm to getLedgerAccontId() which is nothing a container type variable called acctPattern. What this container type variable contains? To get the answer you may refer above commended line. The 1st two values of this container are main account display value and main account number or ledger account, 3rd value is number of dimension. I have 4 dimensions here. The 4th and 5th values are dimension Business unit and its value 001. The 6th and 7th values are dimension Cost center and its value 100. The 8th and 9th values are dimension Division and its value 047. The 10th and 11th values are dimension Location and its value 00001.

I know you must be thinking why to pass hard coded values to container type variable?
By using X++ code you can make it dynamic.
Below code you may use as an example to get container type return. You can create a method at tale or class level.

public container getDimensionPattern(DimensionDisplayValue _displayValue, LedgerJournalAC _accountNum, DimensionDynamicDefaultAccount _defaultDimension)
{
    container                       conAcctStructure;
    RetailMCRChannelTable           RetailMCRChannelTable;
    DimensionAttributeValueSet      dimAttrValueSet;
    DimensionAttributeValueSetItem  dimAttrValueSetItem;
    DimensionAttributeValue         dimAttrValue;
    DimensionAttribute              dimAttr;

    conAcctStructure += _displayValue;
    conAcctStructure += _accountNum;
    conAcctStructure += 4;              //4 Dimensions

    dimAttrValueSet = DimensionAttributeValueSet::find(_defaultDimension);
   
    while select dimAttrValueSetItem
        where   dimAttrValueSetItem.DimensionAttributeValueSet   == dimAttrValueSet.RecId
    {
        dimAttrValue        = DimensionAttributeValue::find(dimAttrValueSetItem.DimensionAttributeValue);
        dimAttr             = DimensionAttribute::find(dimAttrValue.DimensionAttribute);
           
        conAcctStructure += dimAttr.Name;
        conAcctStructure += dimAttrValue.getValue();
    }

    return conAcctStructure;
}


Below screenshot was taken while I was debugging the above method.



Hope this was useful. You may leave your comment or feedback below.

No comments:

Post a Comment