How to add ledger account and offset account (LedgerDimension) into Form in AX 2012

1. In the AOT, locate the "YourTable" and create 2 new fields with the following properties (click on Yes to automatically add a foreign key relation once asked):

Name: Account, OffsetAccount
ExtendedDataType LedgerDimensionAccount

2.  Find the table's relation named DimensionAttributeValueCombination, and change
their property as follows:

UseDefaultRoleNames    No

3.  In the AOT, find the "YourForm", and add the following code to its class declaration:

public class FormRun extends ObjectRun
{
    LedgerDimensionAccountController   dimAccountController;
    LedgerDimensionAccountController   dimOffsetAccountController;
}

4.  Add the following code to the bottom of the form's init()method:
public void init()
{
    super();
    dimAccountController        = LedgerDimensionAccountController::construct(DICHRLedgerAccountSetup_ds, fieldStr(DICHRLedgerAccountSetup,Account));
    dimOffsetAccountController  = LedgerDimensionAccountController::construct(DICHRLedgerAccountSetup_ds, fieldStr(DICHRLedgerAccountSetup,Account));
}


5.  On the same form, created 2 segmented entry controls , set datasource and reference field to Account, OffsetAccount.
Override three of their methods with the following code for segmentedEntry Account control

Public void loadAutoCompleteData(loadAutoCompleteDataEventArgs _e)
{
    super(_e);
    dimAccountController.loadAutoCompleteData(_e);
}

public void loadSegments()
{
    super();
    dimAccountController.parmControl(this);
    dimAccountController.loadSegments();
}

public void segmentValueChanged(SegmentValueChangedEventArgs _e)
{
    super(_e);
    dimAccountController.segmentValueChanged(_e);
}
Do the same for segmentedEntry OffsetAccount control

6.  On the same form, in "YourTable "datasource, locate the
Account, OffsetAccount field and override three of their methods with the following code for each field:

public Common resolveReference(FormReferenceControl _formReferenceControl)
{
    return dimAccountController.resolveReference();
}

public void jumpRef()
{
    super();
    dimAccountController.jumpRef();
}

public boolean validate()
{
    boolean ret;
    ret = super();
    ret = dimAccountController.validate() && ret;
    return ret;
}