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;  

}

Advertisements

Import Dimensions into AX 2012

Its quite common for developer to get the requirement for Importing Default Dimensions into AX 2012,

Below you an find the Dimensions import for Worker in Ax 2012:

Class_WorkerDimImport

Below is the code snippet for the dimensions import for Fixed Assets:

class Mzk_FixedAssetDimensionImport
{
}

Below method will get the dimension recId for the dimensions which we have passed:

private DimensionDefault getNewDefaultDimension(str businessUnit = "" ,
                                                str costcenter = "",
                                                str department = "")
{
    // Dimensions
    #define.businessUnit("BusinessUnit")
    #define.costcenter("Costcenter")
    #define.department("Department")

    Struct                                struct = new Struct();
    container                             ledgerDimension;
    DimensionDefault                      DimensionDefault;
    int i = 0;

    if(businessUnit)
            struct.add(#businessUnit,businessUnit);
    if(costcenter)
        struct.add(#costcenter,costcenter);
    if(department)
        struct.add(#department,department);

    ledgerDimension += struct.fields();

    if(businessUnit)
    {
        i++;
        ledgerDimension += struct.fieldName(i);
        ledgerDimension += struct.valueIndex(i);
    }
    if(costcenter)
    {
        i++;
        ledgerDimension += struct.fieldName(i);
        ledgerDimension += struct.valueIndex(i);
    }
    if(department)
    {
        i++;
        ledgerDimension += struct.fieldName(i);
        ledgerDimension += struct.valueIndex(i);
    }

    DimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);

    return DimensionDefault;

}

Below method isto import Dimensions from Excel sheet:

private boolean importFromExcel(Filename inputFile)
{
    SysExcelApplication         application;
    SysExcelWorkbooks           workbooks;
    SysExcelWorkbook            workbook;
    SysExcelWorksheets          worksheets;
    SysExcelWorksheet           worksheet;
    SysExcelCells               cells;
    AssetBookId                 assetBookId;
    AssetId                     assetId = "";
    AssetBook                   assetBook;
    int row = 1;
    COMVariantType              type;
    str costcenter,department,businessunit;
    int i;

    try
    {
        application = SysExcelApplication::construct();
        workbooks = application.workbooks();

        try
        {
            workbooks.open(inputFile,0,true);
        }
        catch (Exception::Error)
        {
            throw Exception::Error;
        }

        workbook = workbooks.item(1);
        worksheets = workbook.worksheets();
        worksheet = worksheets.itemFromNum(1);
        cells = worksheet.cells();

        type = cells.item(row + 1, 1).value().variantType();

        while (type != COMVariantType::VT_EMPTY)
        {
            row++;
            assetId = "";
            assetBookId = "";
            assetId = cells.item(row,1).value().bStr();
            assetBookId = cells.item(row,2).value().bStr();
            businessunit= cells.item(row,3).value().bStr();
            costcenter = (cells.item(row,4).value().bStr());
            department = (cells.item(row,5).value().bstr());

            ttsBegin;
            select forUpdate assetBook
                        where assetBook.AssetId == assetId
                                          && assetBook.BookId  == assetBookId;
            if(assetBook.RecId)
            {
                if(businessunit || costcenter || department)
                {
                    assetBook.DefaultDimension = this.getNewDefaultDimension(businessunit,costcenter,department);
                    assetBook.update();
                }
                else
                {
                    info(strFmt("Please check the values for AssetId:%1,BookId:%2",assetId,assetBookId));
                }
            }
            else
            {
                info(strFmt("Please check the values for AssetId:%1,BookId:%2",assetId,assetBookId));
            }
            ttsCommit;

            type = cells.item(row + 1, 1).value().variantType();
        }

        application.quit();

        return true;
    }
    catch
    {
        return false;
    }
}

Main method code starts executing from here

public static void main(Args _args)
{
    Dialog                  dialog;
    DialogField             dialogFileName;
    SysOperationProgress    simpleProgress;
    Filename                filename;
    Container               filterCriteria;
    FileIOPermission        permission;
    Mzk_FixedAssetDimensionImport         Dimimport = new Mzk_FixedAssetDimensionImport();
    #File
    #avifiles

    dialog = new Dialog('Select Excel File');
    dialogFileName = dialog.addField("Filenameopen", "File Name");
    filterCriteria = ['*.xls'];
    filterCriteria = dialog.filenameLookupFilter(filterCriteria);
    dialog.run();

    if (dialog.run())
    {
        filename = dialogFileName.value();
        if(!filename)
            info('Filename must be filled');
        else
           Dimimport.importFromExcel(filename);
    }
    info('Done');

}

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<= 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("Business Unit: %1",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
&& dimAttribute.Name == attributeName;
info(strFmt("Business Unit: %1----%2",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<= 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;
}