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

What does this mean: "The form datasource query object does not support changing its AllowCrossCompany property after the form has executed the query."?

I have made a form with datasources vendtable and vendtrans. Inside vendtable_ds.executequery() looks like this: QueryBuildDataSource queryBuildDatasource ,queryBDS_VendTrans_Invoice; ; queryBuildDatasource = this.query().dataSourceTable(tablenum(vendtable)); queryBDS_VendTrans_Invoice = this.query().dataSourceTable(tablenum(vendtrans)); if (curext() == "MASTERCOMP") { this.query().allowCrossCompany(true); } else { this.query().allowCrossCompany(false); } //FilterVendorName = stringedit control on form if (FilterVendorName.text()) { queryBuildDatasource.addRange(fieldNum(VendTable,Name)).value(strfmt("*%1*", FilterVendorName.text())); } else { queryBuildDatasource.clearRange(fieldNum(VendTable,Name)); } //FilterInvoiceNumber = stringedit control on form if (FilterInvoiceNumber.valueStr() == "") { queryBDS_VendTrans_Invoice.enabled(false); } else { queryBDS_VendTrans_Invoice.enabled(true); queryBDS_VendTrans_In...

Credit Note [Dynamics AX] using X++

This post will help to create credit note for a sales order based on the invent lot id. All the invoices raised for a particular sales line – Lot Id will be raised back as a credit note. Information on Credit Note: A credit note or credit memorandum (memo) is a commercial document issued by a seller to a buyer. The seller usually issues a Credit Memo for the same or lower amount than the invoice, and then repays the money to the buyer or sets it off against a balance due from other transactions Below Code will help to create credit note for all the invoices raised against the sales line -lot id. Please note: This code can be customized as per your requirements. This is just a template to help creating credit note using X++ code. Please test the code before use. static void SR_CreateCreditNote_Sales(Args _args) { // Coded by Sreenath Reddy CustInvoiceTrans custInvoiceTrans; Dialog dialog = new Dialog(“Create credit note – for sales.”); DialogField dfInv...