Monday, 9 April 2018


Inventory Transfer Journal through Excel using X++ code in ax 2012

Hi,
This code is used to import Data from Excel. you can make excel according give sequence in below in code.


static void aks_CreateTransferJournal(Args _args)
{
    Dialog                      dialog;
    Filename                    filename;
    DialogField                 dialogFilename;
    //To filter the files while selecting
    container                   conFilter = ["Microsoft Excel 97-2003 Worksheet (.xls)" ,"*.xlsx"];
       SysExcelApplication                 application;
    SysExcelWorkbooks                   workbooks;
    SysExcelWorkbook                    workbook;
    SysExcelWorksheets                  worksheets;
    SysExcelWorksheet                   worksheet;
    SysExcelCells                       cells;
    COMVariantType                      type;
    int                                 row=1;
    ItemId                           itemId;
        InventJournalTable      journalTable;
    InventJournalTableData  journalTableData;
    InventJournalTrans      inventJournalTrans;
    InventDim               toInventDim,frominventDim;
    TransDate                   transDate;
    InventQtyJournal            inventQtyJournal;
    InventBatchId               inventBatchId, toinventBatchId;
    InventDimId                 inventDimId;
    InventSiteId                inventSiteId,toinventSiteId;
    WMSLocationId               wMSLocationId,towMSLocationId;
    InventLocationId            inventLocationId,toinventLocationId;
    ;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    dialog = new dialog();
    dialog.caption("select a file");
    //dialogFilename      =   dialog.addField(typeId(FilenameOpen));
    dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));
    //To filter the files while selecting
    dialog.filenameLookupFilter(conFilter);
    dialog.run();
    if(dialog.closedOk())
    {
        filename = dialogFileName.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();
    do
{
        row++;
                transDate                           = cells.item(row,1).value().date();
                itemId                             = cells.item(row,2).value().bStr();
                inventQtyJournal                    = cells.item(row,3).value().double();
               
                inventSiteId=  cells.item(row,4).value().bStr();
                inventLocationId =  cells.item(row,5).value().bStr();
                inventBatchId = cells.item(row,6).value().bStr();
                wMSLocationId = cells.item(row,7).value().bStr();
               
                toinventSiteId=  cells.item(row,8).value().bStr();
                toinventLocationId = cells.item(row,9).value().bStr();
                toinventBatchId = cells.item(row,10).value().bStr();
                towMSLocationId=  cells.item(row,11).value().bStr();
               
   
            Ttsbegin;


    inventJournalTrans.clear();
    inventJournalTrans.JournalId      = 'AKS-000021';
    inventJournalTrans.JournalType    = InventJournalType::Transfer;
    inventJournalTrans.TransDate      = transDate;
    inventJournalTrans.ItemId         = itemId;
    inventJournalTrans.Qty            = inventQtyJournal ;


   
    inventJournalTrans.initFromInventTable(InventTable::find(inventJournalTrans.ItemId), False, False);
   // Dimensions From which the transfer performs
    frominventDim.InventSiteId = inventSiteId;
    frominventDim.InventLocationId =inventLocationId;
    frominventDim.inventBatchId = inventBatchId;
    frominventDim.wMSLocationId = wMSLocationId;
    // Dimensions To which the transfer performs
    toInventDim.inventSiteId         = toinventSiteId;
    toInventDim.InventLocationId     =  toinventLocationId;
    toInventDim.inventBatchId = toinventBatchId;
    toInventDim.wMSLocationId = towMSLocationId;
    inventJournalTrans.InventDimId = InventDim::findOrCreate(frominventDim).inventDimId;
    inventJournalTrans.ToInventDimId = InventDim::findOrCreate(toInventDim).inventDimId;
    inventJournalTrans.insert();
   
ttsCommit;
              info(strfmt("%1",inventJournalTrans.ItemId));
        type = cells.item(row+1, 1).value().variantType();
        } while(type != COMVariantType::VT_EMPTY);


    workbooks.close();
    application.quit();
    info("Operation/Processing Completed");
}












No comments:

Post a Comment