Update Dimensions for the Item,Customer and Vendor

Some times you may get a strange requirement from client to update few dimensions on Default Dimensions :

Below code snippet is used to update the required dimensions without disturbing the other dimensions


//Bellow example will remove the BU,Dept and CC without distrubing the other dimensions
static void ItemDimUpdate(Args _args)
{
    InventTable inventTable,inventTableselect;
    container dimValue;
    DimensionDefault DimensionDefault,DimensionDefault1;
    str BussinessUnit,CostCenter,Department,ItemGroup,Project;
    int i = 0;

    DimensionAttributeValueSetStorage       dimStorage;
    CustTable                               custTable;
    Counter                                 c;
    

    ttsBegin;
    while select * from inventTableselect
    {
         i = 0;
         BussinessUnit = "";
         CostCenter = "";
         Department = "";
         ItemGroup = "";
         Project = "";
         dim = new Struct();
         dimValue = connull();

        //Getting values from the Old Dimension
        dimStorage = DimensionAttributeValueSetStorage::find(inventTableselect.DefaultDimension);
        for (c=1 ; c<= dimStorage.elements() ; c++)
        {
            switch (DimensionAttribute::find(dimStorage.getAttributeByIndex(c)).Name)
            {
                case 'BussinessUnit':
                    BussinessUnit = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'CostCenter':
                    CostCenter = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'Department':
                    Department = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'ItemGroup':
                    ItemGroup = dimStorage.getDisplayValueByIndex(c);
                break;
                case 'Project':
                    Project = dimStorage.getDisplayValueByIndex(c);
                break;
            }
        }

       //Creating the New Dimension
       //Just keep the required dimensions and Comment the other dimensions

       // if(BussinessUnit)
       //     dim.add('BusinessUnit', BussinessUnit);
       // if(CostCenter)
       //     dim.add('Costcenter', CostCenter);
       // if(Department)
       //     dim.add('Department', Department);
        if(ItemGroup)
            dim.add('ItemGroup', ItemGroup);
        if(Project)
            dim.add('Project', Project);

        dimValue += dim.fields();

      //  if(BussinessUnit)
      //  {
      //      i++;
      //      dimValue += dim.fieldName(i);
      //      dimValue += dim.valueIndex(i);
      //  }
      //  if(CostCenter)
      //  {
      //      i++;
      //      dimValue += dim.fieldName(i);
      //      dimValue += dim.valueIndex(i);
      //  }
      //  if(Department)
      //  {
      //      i++;
      //      dimValue += dim.fieldName(i);
      //      dimValue += dim.valueIndex(i);
      //  }
        if(ItemGroup)
        {
            i++;
            dimValue += dim.fieldName(i);
            dimValue += dim.valueIndex(i);
        }
        if(Project)
        {
            i++;
            dimValue += dim.fieldName(i);
            dimValue += dim.valueIndex(i);
        }

        //Below code will get the new dimension
        DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(dimValue);

        inventTable = inventTable::find(inventTableselect.ItemId,true);
        inventTable.DefaultDimension = DimensionDefault;

        inventTable.update();
    }
    ttsCommit;

    info("Updation completed");
}

 

Below Code snippet is used to update the Item,Customer and Vendor :


static void ItemDimUpdate(Args _args)
{
    InventTable inventTable;
    Struct dim = new Struct();
    container dimValue;
    DimensionDefault DimensionDefault;
    ;  

    dim.add('BusinessUnit', '002');
    dim.add('CostCenter', '007');
    dim.add('Department', '022');
    dim.add('ItemGroup', 'Audio Test');
    dim.add('Project', '000002');  

    dimValue += dim.fields();
    dimValue += dim.fieldName(1);
    dimValue += dim.valueIndex(1);
    dimValue += dim.fieldName(2);
    dimValue += dim.valueIndex(2);
    dimValue += dim.fieldName(3);
    dimValue += dim.valueIndex(3);
    dimValue += dim.fieldName(4);
    dimValue += dim.valueIndex(4);
    dimValue += dim.fieldName(5);
    dimValue += dim.valueIndex(5);  

    ttsBegin;  

    DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(dimValue);
    inventTable = inventTable::find("A0001",true);
    inventTable.DefaultDimension = DimensionDefault;
    inventTable.update();  

    ttsCommit;  

}

Wanna add dimensions to your own form\Add dimensions to existing form

Hi Floks, Today I just want to give you a small code snippets to add Dimensions to your new form or adding them into Ax existing form:

Step 1:

Add a tab/group to your form, let us consider tab\group name is Dimensions

Step 2:

Give appropriate properties to you tab\group in the form Like: Auto Declaration, Data source, Columns, Hide If Empty

Step 3:

Add below lines of code into init() of your form after the super()

dimensionDefaultingController = DimensionDefaultingController::constructInTabWithValues(true,true, true,0,this, Dimensions, "@SYS138487");//Use it for Tab
dimensionDefaultingController = DimensionDefaultingController::constructInGroupWithValues(true,true, true,0,this, Dimensions, "@SYS138487");//Use it for Group
dimensionDefaultingController.parmAttributeValueSetDataSource(prodTable_ds, fieldStr(ProdTable, DefaultDimension));
dimensionDefaultingController.pageActivated();

 

Dimension values from Ledger Dimension

Below code will get you the Default Dimension by passing the Ledger Dimension

public str getBusinessUnit(RecId _ledgerDimension)
{

// DimensionAttributeValueCombination stores the combinations of dimension values
// Any tables that uses dimension combinations for main account and dimensions
// Has a reference to this table’s recid
DimensionAttributeValueCombination dimAttrValueComb;
//GeneralJournalAccountEntry is one such tables that refrences DimensionAttributeValueCombination
GeneralJournalAccountEntry gjAccEntry;
// Class Dimension storage is used to store and manipulate the values of combination
DimensionStorage dimensionStorage;
// Class DimensionStorageSegment will get specfic segments based on hierarchies
DimensionStorageSegment segment;
int segmentCount, segmentIndex;
int hierarchyCount, hierarchyIndex;
str segmentName, segmentDescription;
SysDim segmentValue;
;

dimAttrValueComb = DimensionAttributeValueCombination::find(_ledgerDimension);

// Get dimension storage
dimensionStorage = DimensionStorage::findById(_ledgerDimension);
if (dimensionStorage == null)
{
throw error("@SYS83964");
}

// Get hierarchy count
hierarchyCount = dimensionStorage.hierarchyCount();
//Loop through hierarchies to get individual segments
for(hierarchyIndex = 1; hierarchyIndex <= hierarchyCount; hierarchyIndex++)
{

//Get segment count for hierarchy
segmentCount = dimensionStorage.segmentCountForHierarchy(hierarchyIndex);

//Loop through segments and display required values
for (segmentIndex = 1; segmentIndex <= segmentCount; segmentIndex++)
{
// Get segment
segment = dimensionStorage.getSegmentForHierarchy(hierarchyIndex, segmentIndex);

// Get the segment information
if (segment.parmDimensionAttributeValueId() != 0)
{
// Get segment name
segmentName = DimensionAttribute::find(DimensionAttributeValue::find(segment.parmDimensionAttributeValueId()).DimensionAttribute).Name;
//Get segment value (id of the dimension)
segmentValue = segment.parmDisplayValue();
if(segmentName =="BusinessUnit")            //Give the Dimension Name to that Dimension Value
{
return segmentValue;
}
}
}
}
return "";
}

Getting Dimension values from Default Dimension

From the below  we can get all the Default Dimension values:

Method 1:

DimensionAttributeValueSetStorage dimStorage;
Counter i;
str dimensionValue;
RefRecId defaultdim;
DirPartyName dimensionValue1;

defaultdim = 5637176154; //this number is basically from field DefaultDimension
dimStorage = DimensionAttributeValueSetStorage::find(defaultdim);

for (i=1 ; i&amp;lt;= dimStorage.elements() ; i++)
{
if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name=='BusinessUnit') //Give the Attribute you want to get the value for.
{
dimensionValue = dimStorage.getDisplayValueByIndex(i);
dimensionValue1 = DirPartyTable::findRec(VendTable::find(dimensionValue).Party).Name;//To get the Vend Name
break;
}
}
info(strFmt(&quot;Business Unit: %1&quot;,dimensionValue));

Method 2:


DimensionAttributeValueSet dimAttrValueSet;
DimensionAttributeValueSetItem dimAttrValueSetItem;
DimensionAttributeValue dimAttrValue;
DimensionAttribute dimAttribute;
RefRecID dimensionSetRecID;
Name attributeName;
;
dimensionSetRecID = 5637176154; //this number is basically from field DefaultDimension
attributeName = 'BusinessUnit'; //put here the name of dimension
dimAttrValueSet = DimensionAttributeValueSet::find(dimensionSetRecID);

select dimAttrValueSetItem
where dimAttrValueSetItem.DimensionAttributeValueSet == dimAttrValueSet.RecId
join dimAttrValue
where dimAttrValue.RecId == dimAttrValueSetItem.DimensionAttributeValue
join dimAttribute
where dimAttribute.RecId == dimAttrValue.DimensionAttribute
&amp;amp;&amp;amp; dimAttribute.Name == attributeName;
info(strFmt(&quot;Business Unit: %1----%2&quot;,dimAttrValue.getValue(),dimAttrValue.getName()));

OR

Passing current record buffer


[SysClientCacheDataMethodAttribute(true)]
display DimensionValue BusinessUnit(InventJournalTrans _inventJournalTrans)//Passing current record buffer
{

DimensionAttributeValueSetStorage dimStorage;
Counter i;
DimensionValue dimensionValue;
RefRecId defaultdim;

defaultdim = _inventJournalTrans.DefaultDimension; //this number is basically from field DefaultDimension
dimStorage = DimensionAttributeValueSetStorage::find(defaultdim);

for (i=1 ; i&amp;lt;= dimStorage.elements() ; i++)
{
if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name=='BusinessUnit') //Give the Attribute you want to get the value for.
{
dimensionValue = dimStorage.getDisplayValueByIndex(i);
break;
}
}
return dimensionValue;
}

OR

Adding the Code at Global Class


Eg: To use this method:
tempTable.DimensionValue= getDefaultDimensionValue("BusinessUnit",prodTable.DefaultDimension);
//To Get the default dimension value by using default dimension id
static str 50 getDefaultDimensionValue(str _dimName,DimensionDefault _defaultDimension)
{
 DimensionAttributeValueSetStorage dimStorage;
 CustTable custTable;
 Counter i;
 str returnValue;
 dimStorage = DimensionAttributeValueSetStorage::find(_defaultDimension);
 for (i=1 ; i<= dimStorage.elements() ; i++)
 {
 if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == _dimName)
 {
 returnValue = dimStorage.getDisplayValueByIndex(i);
 break;
 }
 }
 return returnValue;
}

Business Unit Lookup in Form

Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup:

public void lookup()
{
Query query;
SysTableLookup sysTableLookup;
super();

//DimAttributeOMBusinessUnit is a View
sysTableLookup = SysTableLookup::newParameters(tableNum(DimAttributeOMBusinessUnit), this);

sysTableLookup.addLookupfield(fieldNum(DimAttributeOMBusinessUnit, Value));
sysTableLookup.addLookupfield(fieldNum(DimAttributeOMBusinessUnit, Name));

query = new Query();

query.addDataSource(tableNum(DimAttributeOMBusinessUnit));

sysTableLookup.parmQuery(query);

sysTableLookup.performFormLookup();

}