web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

X++ code to import Depreciation Book data for Fixed Assets in ax2012

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

Below job can be used to import base information of depreciation book data (Path: Fixed assets/Common/Fixed assets/Fixed assets/ Depreciation Books) for a given set of fixed asset Names from a spreadsheet. Input spreadsheet is assumed to have only two columns namely FixedAssetName and DepreciationBookId and holding values for the same. Filepath is assumed to be in C drive with name DepBookNames.xlsx.

Input:

DepBooks.png

static void CG_ImportDepBooks(Args _args)
{
    AssetDepBook            assetDepBook;
    AssetTable              assetTable;
    AssetDepBookTable       assetDepBookTable;
    SysExcelApplication     application;
    SysExcelWorkbooks       workbooks;
    SysExcelWorkbook        workbook;
    SysExcelWorksheets      worksheets;
    SysExcelWorksheet       worksheet;
    SysExcelCells           cells;
    COMVariantType          type;
    AssetId                 assetId;
    FileName                filename;
    FileIOPermission        permission;
    AssetDepreciationBookId assetDepBookId;
    int                 row = 1 ;
    #File

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

    filename = @"C:\\DepBookNames.xlsx";
    permission = new FileIOPermission(fileName, #io_read);
    permission.assert();

    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();

    do
    {
        row++;

        assetId = cells.item(row, 1).value().bStr();
        select assetTable
            where assetTable.AssetId == assetid;

        if (assetTable.RecId)
        {
            assetDepBookId = cells.item(row, 2).value().bStr();
            assetDepBookTable = AssetDepBookTable::find(assetDepBookId);

            if (assetDepBookTable.RecId)
            {
                if (AssetDepBook::find(assetDepBookId, assetTable.AssetId).RecId)
                {
                    info(strFmt("Depreciation bookId %1 is already connected with AssetId %2 at row %3", assetDepBookId, assetTable.AssetId, row));
                }
                else
                {
                    assetDepBook.AssetId = assetTable.AssetId;
                    assetDepBook.DepreciationBookId = assetDepBookId;
                    assetDepBook.DepreciationStartDate = today();
                    assetDepBook.insert();
                }
            }
            else
            {
                info(strFmt("Depreciation bookId %1 is invalid or does not exist at row %2", assetDepBookId, row));
            }
        }
        else
        {
            info(strFmt("Asset %1 is invalid or does not exist at row %2", assetid, row));
        }

        type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
        application.quit();

    CodeAccessPermission::revertAssert();

    info("Operation completed");
}

Comments

*This post is locked for comments