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

Mark All for Open Cust Trans and Open Vend Trans

We have situations where there are lots of open transactions that need to be settled against each other. This can be the case if auto settlement is turned off. One solution is to add a "Mark All" button to the custOpenTrans or vendOpenTrans forms. This button "checks" the mark checkbox on every line. The user can then uncheck several lines if needed and Update to settle the lines. The code below is an example of what we used on the open vendor transaction screen. The code is very similar on the AR side. One note: I used vendTable.AccountNum in the code below. That should be generalized to work with any buffer that is passed into the open trans form. void customMarkAll() { VendTransOpen localVendTransOpen; VendTrans localVendTrans; container conSum; int linesProcessed; ; //show wait cursor startLengthyOperation(); element.lock(); //remove all prior markings specOffsetVoucher.deleteSpe...

Print Report in Microsoft Dynamics AX 2009 through X++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following code: void clicked() {     Args                args;     ReportRun           reportRun;     SalesFormLetter     salesFormLetter;     PrintJobSettings    printJobSettings;     CustConfirmJour     custConfirmJour;     RecordSortedList    list                = new RecordSortedList(55);     SalesTable          salesTableUpdate;     ;     SELEC...

Creating Free Text Invoice through X++ code

Job: Calling class from job to run the class public void freeTextInvoicePostTestJob() { Dialog dialog; DialogField dlgCustAcc; DialogGroup dialogPeriodLengthGroup, dialogPeriodLengthGroup1; DialogField dlgLedgerAcc; ; dialog = new Dialog("Free-Text Invoice"); dialogPeriodLengthGroup1 = dialog.addGroup('Cust Table'); dlgCustAcc = dialog.addField(typeid(CustAccount)); dialogPeriodLengthGroup = dialog.addGroup('Ledger Table'); dlgLedgerAcc = dialog.addField(typeid(LedgerAccount)); if(dialog.run()) { if(dlgCustAcc.value() && dlgLedgerAcc.value() != '') FreeTxtInvoiceCreatePost::main(dlgCustAcc.value(), dlgLedgerAcc.value()); else throw error(strfmt("Either CustAccount or LedgerAccount info is missing.")); } } Class: Which creates the free text invoice class FreeTxtInvoiceCreatePost { } static void main(CustAccount _custAccount, LedgerAccount _ledgerAccount) { CustInvoiceTable custInvoiceTable; ...