2
votes

I have the following problem: the following code works perfectly:

inventJournalTrans.clear();
inventJournalTrans.initFromInventJournalTable(inventJournalTable);
inventJournalTrans.ItemId = "100836M";

frominventDim.InventLocationId="SD";
frominventDim.wMSLocationId = '11_RECEPTION';
fromInventDim.InventSizeId = '1000';
fromInventDim.inventBatchId = 'ID057828-CN';

ToinventDim.InventLocationId = "SD";
ToInventDim.wMSLocationId = '11_A2';
ToInventDim.InventSizeId = '1000';
ToInventDim.inventBatchId = 'T20/0001/1';

ToinventDim = InventDim::findOrCreate(ToinventDim);
frominventDim = InventDim::findOrCreate(frominventDim);

inventJournalTrans.InventDimId = frominventDim.inventDimId;
inventJournalTrans.initFromInventTable(InventTable::find("100836M"));
inventJournalTrans.Qty = -0.5;
inventJournalTrans.ToInventDimId = ToinventDim.inventDimId;
inventJournalTrans.CostAmount = InventJournalTrans.calcCostAmount(-abs(any2real(strReplace('-0.5',',','.'))));
inventJournalTrans.TransDate = SystemDateget();
inventJournalTrans.insert();

inventJournalCheckPost = InventJournalCheckPost::newJournalCheckPost(JournalCheckpostType::Post,inventJournalTable);
inventJournalCheckPost.parmThrowCheckFailed(_throwserror);
inventJournalCheckPost.parmShowInfoResult(_showinforesult);
inventJournalCheckPost.run();

It creates transfer journal line correctly and posts the transfer journal successfully.

My requirement is to import journal lines from a csv file. I wrote the following code:

    inventJournalTrans.clear();
    inventJournalTrans.initFromInventJournalTable(inventJournalTable::find(_journalID));
    InventJournaltrans.ItemId             = conpeek(_filerecord,4);

    inventDim_From.InventLocationId       = 'SD';
    inventDim_From.wMSLocationId          = '11_RECEPTION';
    InventDim_from.InventSizeId           = conpeek(_fileRecord,11);
    InventDim_From.inventBatchId          = strfmt("%1",conpeek(_fileRecord,5));

    InventDim_To.InventLocationId         = 'SD';
    inventDim_To.wMSLocationId            = strfmt("%1",conpeek(_fileRecord,10));
    InventDim_To.InventSizeId             = conpeek(_fileRecord,11);
    InventDim_To.inventBatchId            = strfmt("%1",conpeek(_fileRecord,6));

    InventDim_From                        = InventDim::findOrCreate(inventDim_From);
    inventDim_To                          = InventDim::findOrCreate(inventDim_To);

    InventJournalTrans.InventDimId = inventDim_From.inventDimId;
    InventJournalTrans.initFromInventTable(InventTable::find(conpeek(_filerecord,4)));
    inventJournalTrans.Qty                = -abs(any2real(strReplace(conpeek(_fileRecord,8),',','.')));
    inventJournalTrans.ToInventDimId      = inventDim_To.inventDimId;
    InventJournalTrans.CostAmount         = InventJournalTrans.calcCostAmount(-abs(any2real(strReplace(conpeek(_fileRecord,8),',','.'))));
    inventJournalTrans.TransDate          = str2date(conpeek(filerecord,9),123);
    InventJournalTrans.insert();

I have the following error when I use the insert() method : size does not exists for the itemId. When I have a look in inventSize table for my itemId, the size exists, I thought it was an inventDimId problem in inventJournalTrans but they're strictly similar to the first code exemple. All my datas are the same as the first exemple but are not hard-coded and come from reading my csv file.

I spend a lot of time debugging and found nothing wrong but error message remains I'm using Dynamics AX V4 SP1. Thanks a lot for any help.

1
Are you sure that the ItemId is being retrieved as expected from the file? - SShaheen
Which version are you using? - FH-Inway
Have you tried the following while debugging: First, replace all the values in the code that reads the csv file with hard coded values so that it works. Then, one by one, replace the values with the values from the csv file. Check after each replacement that the code still works. This way, you can identify which value causes the issue. Then, write the value into a separate variable and compare it with a variable that holds the hardcoded value. Compare the two values in code to make sure they are equal. And just as a wild guess: check that there are no encoding issues with the csv file. - FH-Inway
The second to last line, you have inventJournalTrans.TransDate = str2date(conpeek(filerecord,9),123);. Should it be _filerecord and not filerecord? Is this AX 2009 or 2012? - Alex Kwitny
While debugging, throw a conView(_filerecord) at the top, and use that to verify the contents of the CSV data. It sounds like you have an invalid Item+Size combination, so while the size may exist, it may not be valid for that item. - Alex Kwitny

1 Answers

0
votes

When you read from files always trim trailing spaces. You can do that using the strRtrim function.

Like so:

InventDim_To.InventSizeId = strRtrim(conpeek(_fileRecord,11));