Friday, March 1, 2013

Ax 2012 changing financial dimensions

static void HKS_defaultSODimension(Args _args)
{

    DimensionAttribute                    dimensionAttribute;
    DimensionAttributeValue               dimAttributeValue; 
    DimensionAttributeValueSetStorage    dimensionAttributeValueSetStorage;
    DimensionAttributeValueSetItem       dimensionAttributeValueSetItem; 
    DimensionAttributeValueSet            dimAttrValueSet; //Stores the Hash value based on the combination of Atttibutes values. If no record found, new record is created.
    SalesTable                            salesTable;

    recId                           defaultDim;
    TableId                         viewId;

    viewId = tableNum("DimAttributeSalesTable");

    select firstonly  dimensionAttribute where dimensionAttribute.BackingEntityType == viewId;
    //dimensionAttribute = DimensionAttribute::findByName("VendorGroup"); //find the attribute table.
    dimAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,"20",false,true); //get the Attribute value table. If the value is not found create a new one.

    salesTable = salesTable::find("SO-100605");
    dimensionAttributeValueSetStorage = dimensionAttributeValueSetStorage::find(salesTable.DefaultDimension); 
    //if ( !dimensionAttributeValueSetStorage.getDisplayValueByDimensionAttribute(dimensionAttribute.RecId))
    if (dimAttributeValue) //Check if the attribute value is valid.
    {    
        //checkif the attribute value is already exisitng as part of hash value in dimensionAttributeValueSetStorage
        if (!dimensionAttributeValueSetStorage.containsDimensionAttributeValue(dimAttributeValue.RecId)) 
        {
            dimensionAttributeValueSetStorage.addItem(dimAttributeValue);
            defaultDim =  dimensionAttributeValueSetStorage.save();
            update_recordSet salesTable setting  DefaultDimension =  defaultDim where salesTable.SalesId == "SO-100605"; //update the reference dimension.
        }
    }
}