AX – D365FO – Importing CSV Using RunBaseBatch X++

class ImportCsv extends Runbasebatch
{
	Filename        ItemFileName;
	Filename        filename;
	DialogField     dialogFilename;
	#define.CurrentVersion(1)
	#define.Version1(1)
	
	#localmacro.CurrentList
		fileName
	#endmacro
}

public Object dialog()
{
	DialogRunbase       dialog = super();
	
    dialogFilename   = dialog.addField(typeId(FilenameOpen));
	dialogFilename.value(filename);
    
	return dialog;
}

public boolean getFromDialog()
{
	fileName = dialogFileName.value();
	
	return super();
}

void ImportInventtable()
{
	CommaIo                 file = new commaIo(ItemFileName,'r');
	Container               con;
	InventTable             inventTable;
	int                     x,y;
	
        file.inFieldDelimiter(',');
	if (file)
	{
		ttsbegin;
		while(file.status() == IO_Status::OK)
		{
			con = file.read();
			if (con)
			{
				Inventtable = Inventtable::find(conpeek(con,2));
				if(Inventtable)
				{
					Inventtable.InventTableId     =   conpeek(con,1);
					Inventtable.ItemId            =   conpeek(con,2);
					Inventtable.Name              =   conpeek(con,4);
					Inventtable.validateWrite();
					Inventtable.insert();
					x++;
			}
			else
			{
				y++;
			}
			}
		}
	}

	ttscommit;
	info(strfmt("%1 record(s) imported, %2 record(s) not found",x,y));
}

public container pack()
{
	return [#CurrentVersion,#CurrentList];
}
public void run()
{
	this.ImportInventtable();
	super();
}

public boolean unpack(container packedClass)
{
	Version version = runbase::getVersion(packedClass);
	
	switch (version)
	{
		case #CurrentVersion:
			[version,#CurrentList] = packedClass;
			break;
		default:
			return false;
	}
	return true;
}

public boolean validate()
{
	if (false)
	return checkFailed("");
	return true;
}
static void main(Args _args)
{
	ImportCsv      ImportCsv ;
	FormRun formRun;
	Args    args;
	
	ImportCsv = new ImportCsv ();
	if (ImportCsv.prompt())
	{
		ImportCsv.run();
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s