Skip to main content

Inventory Journal Import

1. Create a Movement Journal and go into the Lines screen.

2. Select Functions, Import Lines



3. Select a csv file with the columns: ItemId, Warehouse, Location, Batch, Qty, Cost. (you can mod the code to expand this)



4. The lines are added to the journal.



You can download the xpo below, and here are some of the key bits of code.



Create a class (mine is called inventJournalImport) extending runbase. Create a menu item (action) for the class and drop it on the InventJournalMovement form.



The main method of the class is as follows:

client static void main(Args args)
{
inventJournalImport inventJournalImport;
Object formRunObject;
JournalForm journalForm;
InventJournalId inventJournalId;
FormDataSource journalTrans_ds;
;

inventJournalImport = new inventJournalImport();
inventJournalImport.getLast();

if (!args || !args.caller() || args.caller().name() != formStr(InventJournalMovement))
{
throw error(strfmt("This function must be called from the %1 form.", formStr(InventJournalMovement)));
}

if (formHasMethod(args.caller(), identifierstr(journalForm)))
{
formRunObject = args.caller();

journalForm = formRunObject.journalForm();
inventJournalImport.parmJournalForm(journalForm); //needed to index numOfLines on inventJournalTable

inventJournalId = journalForm.journalTableData().journalTable().JournalId;
inventJournalImport.parmInventJournalId(inventJournalId); //for defaulting on the imported lines

journalTrans_ds = journalForm.journalTransData().journalTrans().dataSource(); //for .executeQuery, below
}

if (inventJournalImport.prompt())
{
inventJournalImport.ImportRecords();

//refresh the grid
journalTrans_ds.executeQuery();
}
}


The ImportRecords method is below. There is a dialog in the class that fills filename and transactionDate.

void ImportRecords()
{
AsciiIo asciiIo;
container con;
FileIoPermission fioPermission;

InventJournalTrans inventJournalTrans;
InventDim inventDim;

wmsLocationId wmsLocationId;
inventLocationId inventLocationId;
inventBatchId inventBatchId;

boolean useDefaultInventDim, useDefaultCost;
int imported;
;

if (WINAPI::fileExists(fileName))
{
//show wait cursor
startLengthyOperation();

// The AsciiIO.new method runs under code access permission.
fioPermission = new FileIoPermission(fileName,"R");

if (fioPermission == null)
{
info(strfmt("Not able to read file: %1",fileName));
return;
}

// Code access permission scope starts here.
fioPermission.assert();

ttsbegin;

asciiIo = new AsciiIo(fileName,"R");
asciiIo.inFieldDelimiter(",");
if (asciiIo != null)
{
con = asciiIo.read();

while (asciiIo.status() == IO_Status::Ok)
{
try
{
//1-itemId, 2-warehouse, 3-location, 4-batch, 5-qty, 6-cost
inventJournalTrans.clear();
inventJournalTrans.TransDate = transactionDate;
inventJournalTrans.JournalId = journalId;
inventJournalTrans.initFromInventJournalTable(inventJournalTrans.inventJournalTable());

inventJournalTrans.ItemId = strLTrim(strRTrim(conpeek(con,1)));

inventLocationId = strLTrim(strRTrim(conpeek(con,2)));
wmsLocationId = strLTrim(strRTrim(conpeek(con,3)));
inventBatchId = strLTrim(strRTrim(conpeek(con,4)));
if (inventLocationId || wmsLocationId || inventBatchId)
{
useDefaultInventDim = false;

inventDim.clear();
inventDim.InventLocationId = inventLocationId;
inventDim.wMSLocationId = wmsLocationId;
inventDim.inventBatchId = inventBatchId;
inventDim = InventDim::findOrCreate(inventDim);

inventJournalTrans.InventDimId = inventDim.inventDimId;
}
else
{
useDefaultInventDim = true;
}

inventJournalTrans.Qty = str2num(strLTrim(strRTrim(conpeek(con,5))));
inventJournalTrans.Qty= decround(inventJournalTrans.Qty,InventTable::inventDecimals(inventJournalTrans.ItemId));

if (inventJournalTrans.Qty > 0)
{
//import the cost if this is an addition to inventory
useDefaultCost = false;
inventJournalTrans.CostPrice = str2num(strLTrim(strRTrim(conpeek(con,6))));
inventJournalTrans.PriceUnit = 1;
inventJournalTrans.CostMarkup = 0;
inventJournalTrans.CostAmount = inventJournalTrans.calcCostAmount();
}
else
{
useDefaultCost = true;
}

inventJournalTrans.initFromInventTable(inventJournalTrans.inventTable(),false, useDefaultInventDim, useDefaultCost);

inventJournalTrans.insertFromCode();
imported++;

//updates the number of lines count on the journal table
journalForm.journalTableData().addTotal(inventJournalTrans);
}
catch (Exception::Error)
{
exceptionTextFallThrough();
}

con = asciiIo.read();
}
}

// revertAssert is not really necessary here because the method is ending.
CodeAccessPermission::revertAssert();

ttscommit;
info (strfmt("%1 records imported", imported));

//remove wait cursor
endLengthyOperation();
}
else
{
error(strfmt("Import File not found: %1",fileName));
}
}

Popular posts from this blog

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future. AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable"); AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim"); AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable"); rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'"); if (MySalesOrderObject.CurrencyCode.Trim().Length == 0) MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim(); string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim(); //set header level fields axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim()); axSalesTable.Call("parmCustAccount", M

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }