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 :
Microsoft Dynamics AX (Archived)

How to update InventSerial Table using X++ code

(0) ShareShare
ReportReport
Posted on by 518

Hello everyone,

The below code inserts the data from excel to InventSerial table but i want to update InventSerial table if any difference is there between the InventSerial table and excel data.

Thank You

public void importExcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row = 1;

InventSerial inventSerial;
;

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

try
{
workbooks.open(fileNameOpen);
}
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++;
inventSerial.InventSerialId = cells.item(row, 1).value().bStr();
inventSerial.ItemId = cells.item(row, 2).value().bStr();
inventSerial.ProdDate = cells.item(row, 3).value().date();
inventSerial.RFIDTagStatus();
//info(strfmt('%1 - %2', itemId, name));
type = cells.item(row+1, 1).value().variantType();
inventSerial.insert();
}

while (type != COMVariantType::VT_EMPTY);
application.quit();

}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at
    RE: How to update InventSerial Table using X++ code

    No, your code is wrong.

    It's should looks like this:

        SysExcelApplication application;
        SysExcelWorkbooks   workbooks;
        SysExcelWorkbook    workbook;
        SysExcelWorksheets  worksheets;
        SysExcelWorksheet   worksheet;
        SysExcelCells       cells;
        COMVariantType      type;
        int                 row = 1;
        int                 rows;
        
        application = SysExcelApplication::construct();
        workbooks   = application.workbooks();
    
        try
        {
            workbooks.open(fileNameOpen);
        }
        catch (Exception::Error)
        {
            throw error("File cannot be opened.");
        }
    
        workbook   = workbooks.item(1);
        worksheets = workbook.worksheets();
        worksheet  = worksheets.itemFromNum(1);
        cells      = worksheet.cells();
    
        do
        { 
            ttsBegin;
            
            select forupdate inventSerial 
                where inventSerial.InventSerialId != cells.item(row, 1).value().bStr() 
                   || inventSerial.ItemId         != cells.item(row, 2).value().bStr();
        
            if(inventSerial)
            {
                inventSerial.InventSerialId = cells.item(row, 1).value().bStr();
                inventSerial.ItemId         = cells.item(row, 2).value().bStr();
                inventSerial.ProdDate       = cells.item(row, 3).value().date();
                inventSerial.RFIDTagStatus();
                InventSerial.update();
            }
            else
            {
                inventSerial.clear();
                inventSerial.InventSerialId = cells.item(row, 1).value().bStr();
                inventSerial.ItemId         = cells.item(row, 2).value().bStr();
                inventSerial.ProdDate       = cells.item(row, 3).value().date();
                inventSerial.RFIDTagStatus();
                inventSerial.insert();
            }
            ttsCommit;
            
            type = cells.item(row + 1, 1).value().variantType();
            row++;
        }
        while (type != COMVariantType::VT_EMPTY);
        
        application.quit();


  • Siddhant Singh Profile Picture
    518 on at
    RE: How to update InventSerial Table using X++ code

    Hello Rustem;

    Thanks for your reply. I am getting an error . ==> Method 'value' in COM object of class 'Range' returned error code 0x800A01A8 (<unknown>) which means: <unknown>. Is i written the code correctly.

    Thank You,

    Siddhant Singh

    public void importExcel(Args _args)
    {
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row = 1;

    ;

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

    try
    {
    workbooks.open(fileNameOpen);
    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened.");
    }

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

    inventSerial.clear();
    while select forupdate inventSerial where inventSerial.InventSerialId != cells.item(row, 1).value().bStr() ||
    inventSerial.ItemId != cells.item(row, 2).value().bStr()
    {
    if(inventSerial)
    {

    do
    {
    ttsBegin;
    row++;
    inventSerial.InventSerialId = cells.item(row, 1).value().bStr();
    inventSerial.ItemId = cells.item(row, 2).value().bStr();
    inventSerial.ProdDate = cells.item(row, 3).value().date();
    inventSerial.RFIDTagStatus();
    type = cells.item(row+1, 1).value().variantType();
    inventSerial.insert();
    InventSerial.update();
    ttsCommit;
    }

    while (type != COMVariantType::VT_EMPTY);
    }
    application.quit();
    }
    }

  • Rustem Galiamov Profile Picture
    8,072 on at
    RE: How to update InventSerial Table using X++ code

    Did you solve the issue?

  • Verified answer
    Rustem Galiamov Profile Picture
    8,072 on at
    RE: How to update InventSerial Table using X++ code

    Hi Siddhant Singh!

    You should use ttsbegin, ttscommit and select forupdate statement to retrieve data from InventSerial table and then compare that data with excel data and then do update or insert.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Syed Haris Shah Profile Picture

Syed Haris Shah 9

#2
Mea_ Profile Picture

Mea_ 4

#3
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans