How to import CSV file with Progress bar indication in Ax 2012 & Ax 2009

static void ImportCSVwithProgresBar(Args _args)

{

Filename                    fileName;

container                   files,inRecord,toTRec;

CommaIO                     commaIO;

Dialog                      _dialog;

DialogField                 _dialogFieldFileName;

counter                     counter = 0;

counter                     ErrCounter = 0;

counter                     progressCounter = 0;

SysOperationProgress        progress = new SysOperationProgress();

 

InventTable                 inventTable;

ItemID                      searchItem;

int                         totRows;

 

TextBuffer tb;

;

 

_dialog              = new Dialog(‘ImportCSV’);

_dialogFieldFileName = _dialog.addField(ExtendedTypeStr(FileNameOpen), ‘@SYS53125’, ‘@JKI58’);

_dialogFieldFileName.lookupButton(2);

 

if (_dialog.run())

{

Setprefix(‘@JKI57’);

fileName = _dialogFieldFileName.value();

if(!fileName)

{

info(strfmt(‘@SYS18624’));

return;

}

 

tb = new TextBuffer();

tb.fromFile(fileName);

totRows = tb.numLines();

 

commaIO = new CommaIO(fileName, ‘@SYS76352’);

inRecord = commaIO.read();

progress.setCaption(“@SYS4128”);

#AviFiles

progress.setAnimation(#AviUpdate);

inRecord = commaIO.read();

 

progress.setTotal(totRows);

//progress.setText(strfmt(“Updating Item”, searchItem));

 

ttsbegin;

while (!commaIO.status())

{

searchItem = (conPeek(inRecord, 1));

SetPrefix (strfmt(‘@JKI32’, (progressCounter+1), searchItem));

 

select firstonly forupdate inventTable where inventTable.ItemId == searchItem;

if (inventTable)

{

 

if (InventTable.DimGroupId != “@JKI59” && InventTable.DimGroupId != “@JKI60”)

{

inRecord = commaIO.read();

warning(strfmt(“@JKI61”, InventTable.DimGroupId));

continue;

}

 

inventTable.PdsShelfLife  = str2num(conPeek(inRecord, 2));

inventTable.PdsShelfAdvice = str2num(conPeek(inRecord, 3));

inventTable.PdsBestBefore = str2num(conPeek(inRecord, 4));

 

if (inventTable.validateWrite())

{

counter++;

//inventTable.update();

info(strfmt(“@JKI62”, SearchItem));

}

else

{

ErrCounter++;

error(strfmt(“@JKI63”, progressCounter));

}

}

else

{

warning(strfmt(“@JKI64”, SearchItem));

}

inRecord = commaIO.read();

progressCounter++;

progress.setText(strfmt(“Updating Item %1%”, (progressCounter/totRows)*100));

progress.incCount();

}

 

if (!ErrCounter)

{

ttsCommit;

info(strfmt(‘@JKI66’, counter));

}

else

{

ttsabort;

error(‘@JKI47’);

info(strfmt(‘@JKI67’, ErrCounter));

}

info(strfmt(‘@JKI65’, progressCounter));

}

}

 

Advertisements

Import Csv file to Ax 2012 using X++ code and as Runbase batch

class John_RBTestClass extends RunBaseBatch
{
}

client server static ClassDescription description()
{
return “Johnkrish Batch Test”;
}

static void main(Args _args)
{
John_RBTestClass objClass = new John_RBTestClass();
//prompt for runbase framework dialog
if (objClass.prompt())
{
//run the process
objClass.run();
}
}

 

public void run()
{
Bill_Copy bc;

#File
IO iO;
CustAccount custAccount;
CustName custname;
str one;
FilenameOpen filename = @’E:\Johnkrish\Johnkrish.csv’;//To assign file name
Container record;
boolean first = true;

str Delimiter = “,”;
int totalRecords;
;
iO = new CommaTextIo(filename,#IO_Read);
if (! iO || iO.status() != IO_Status::Ok)
{
throw error(“@SYS19358”);
}

record = iO.read();

while(iO.status() == IO_Status::Ok)

{

record = iO.read();

if(!record)

break;
bc.CustName=conpeek(record, 1);
bc.ItemName=conpeek(record, 2);
bc.insert();

}

}

DATA in .Csv file
John,Mobile
Krish,Phone
Gopal,Purse