Pages

Banner 468 x 60px

 

Thursday, February 8, 2018

Update Default Financial Dimension in AX 2012 using X++

0 comments

Update Default Financial Dimension in AX 2012 using X++ 


static void ABUDefaultFinancialDim_AP(Args _args)
{
    DimensionAttributeValue             dimAttrBUValue,dimAtrrCCValue,dimAtrrDepValue,dimAttrIGValue,dimAtrrProjValue;
    DimensionAttribute                  dimAttrBU,dimAtrrCC,dimAtrrDep,dimAttrIG,dimAtrrProj;
    DimensionAttributeValueSetStorage   davss;
    RecId                               defaultDimension;
    AssetBook                           Assetbook;
    AssetId                             AssetId;
    str                                 A_SITES, B_BRANCHES, C_DEPARTMENTS;

    SysExcelApplication         application;
    SysExcelWorkbooks           workbooks;
    SysExcelWorkbook            workbook;
    SysExcelWorksheets          worksheets;
    SysExcelWorksheet           worksheet;
    SysExcelCells               cells;
    COMVariantType              type1;
    Name                        name;
    HcmWorker                   HCMWorker;
    HcmPersonnelNumberId        HcmPersonnelNumberId;
    FileName                    filename;
    CustTable                   CustTable;
    CustAccount                 CustAccount;
    DirPartyName                DirPartyName;
    CustPriceGroup              PriceGroup;
    DirPartyTable               dirPartyTable1;
    LogisticsPostalAddress      postalAddress;
    LogisticsLocation           location;
    LogisticsLocationRole       locationRole;
    DirPartyLocationRole        partyLocationRole;
    DirPartyLocation            partyLocation;


    int                         row;
    str                         currency;
    str                         site,warehouse,payterid,paymode;
    str  100                    Address,Description;
    Dialog                      _dialog;
    DialogField                 _file;

     str COMVariant2Str(COMVariant _cv, int _decimals = 0,int _characters = 0,int _separator1 = 0,int _separator2 = 0)
       {
            switch(_cv.variantType())
            {
                case (COMVariantType::VT_BSTR):
                    return _cv.bStr();
                case (COMVariantType::VT_R4):
                    return num2str(_cv.float(),_characters,_decimals, _separator1,_separator2);
                case (COMVariantType::VT_R8):
                    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
                case (COMVariantType::VT_DECIMAL):
                    return num2str(_cv.decimal(),_characters,_decimals, _separator1, _separator2);
                case (COMVariantType::VT_DATE):
                    return date2str(_cv.date(),123,2,1,2, 1,4);
                case (COMVariantType::VT_EMPTY):
                    return "";
                default:
                    throw error(strfmt("@SYS26908",_cv.variantType()));
            }
            return "";
        }
    ;

    _dialog = new Dialog("Please select the file to load");
    _dialog.addText("Select file:");
    _file = _dialog.addField(ExtendedTypeStr("FilenameOpen"));
    _dialog.run();

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    filename = _file.value();
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    row = 1;
    do
    {
        row++;

        Assetid                      = cells.item(row, 1).value().bStr();
        A_SITES                      = cells.item(row, 2).value().bStr();
        B_BRANCHES                   = cells.item(row, 3).value().bStr();
        C_DEPARTMENTS                = cells.item(row, 4).value().bStr();
       
        davss = DimensionAttributeValueSetStorage::find(AssetBook::find(Assetid, 'Commercial').DefaultDimension);

        dimAttrBU = DimensionAttribute::findByName('A_SITES');
        dimAtrrCC = DimensionAttribute::findByName('B_BRANCHES');
        dimAtrrDep = DimensionAttribute::findByName('C_DEPARTMENTS');

        dimAttrBUValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttrBU, A_SITES, false, true);
        dimAtrrCCValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAtrrCC, B_BRANCHES, false, true);
        dimAtrrDepValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAtrrDep, C_DEPARTMENTS, false, true);   

        if(dimAttrBUValue || dimAtrrCCValue ||dimAtrrDepValue)
        {
            ttsBegin;
            davss.addItem(dimAttrBUValue);
            davss.addItem(dimAtrrCCValue);
            davss.addItem(dimAtrrDepValue); 
       
            select forUpdate Assetbook where Assetbook.AssetId == Assetid;
            Assetbook.DefaultDimension = davss.save();
            Assetbook.update();
            ttsCommit;
           
            info(strFmt('Asset id %1 - DefaultDimension %2', AssetId, Assetbook.DefaultDimension));
        }

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

    }
    while (type1 != COMVariantType::VT_EMPTY);

    application.quit();

}

This is template for update DefaultDimension fixed Asset (AssetTable) :




0 comments:

A financial dimension value is based on the LAND-00013 record and has been used on a transaction. You cannot delete the LAND-00013 record AX 2012

 A financial dimension value is based on the LAND-00013 record and has been used on a  transaction. You cannot delete the LAND-00013 record ...