Journal import - Record already exists ?

Hi

I have created an import tool (got the code from a nice blogger, Thanks!) and changed it slightly to suit my needs, the import woks fine bu the journal cannot be posted.

I get this error: Cannot create a record in Hours (ProjEmplTrans). Project: P00007, 2010-09-09.
The record already exists.

Which offcourse is not true, at least not in that table this record does not exsit. I suspect my number sequence assignment might be wrong. Any tip would be great.

Here’s the code:

``

public void InsertProjHours()
{
#define.ioComma(";")
#define.ioEndLine("\r\n")
#define.ioRead("r")

`

AsciiIo io ;
real TotalHours;

str jourName;
str 10 jourNum;
str lineNum;
str projectId;
str costprice;
str salesprice;
str lineprop;
str category;
str salescurrency;
str empl;
str qty;
str transdate;
str projtransdate;
str businessline;
str costcenter;
str transtxt;
str fileName;

int linecount;

ProjJournalTrans projJournalTrans;
ProjJournalTable projJournalTable;
LedgerJournalACType ledgerJournalACType;

NumberSeq numSeq;
NumberSeq numSeqJour;
int n = 1,idx =1;
SysOperationProgress lprogress;
#AviFiles

;
fileName = StringEditFileName.valueStr();
jourName = ComboBoxJournalName.valueStr();

io = new AsciiIo(fileName, #ioRead);
if(!io)
error (“File cannot be open”);

lprogress = new SysOperationProgress();
lprogress.setCaption("Insert General ledger trans ");
lprogress.setAnimation(#AviUpdate);

//Create the journal
numseqJour = NumberSeq::newGetNumFromCode(ProjParameters::numRefProjJournalId().NumberSequence);
projJournalTable.initFromProjJournalName(ProjJournalName::Find(jourName));
journum = numseqJour.num();
projJournalTable.JournalNameId = jourName;
projJournalTable.Description = ProjJournalName::Find(jourName).Description;
projJournalTable.VoucherSeqId = ProjJournalName::Find(jourName).VoucherSeqId;
projJournalTable.JournalId = journum;
projJournalTable.insert();

//Create lines
if (io)
{
io.inFieldDelimiter(#ioComma);
io.inRecordDelimiter(#ioEndLine);
[projtransdate, salescurrency, costprice, projectid, salesprice ,qty, transtxt, businessline, costcenter, lineprop, category, empl] = io.read();

while (io.status() == IO_Status::Ok)
{
if (io.status() != IO_Status::Ok)
{
error(“File not found”);
}
TotalHours += str2Num(qty);
ttsbegin;
lprogress.setCount(n,Idx);
lprogress.setText(“Inserting trans”);
n++;
Idx++;

numSeq = NumberSeq::newGetVoucherFromCode(ProjJournalName::find(jourName).VoucherSeqId);
if(!numSeq)
{
error(strfmt(“Number sequence set up does not exists for journal %1”,jourName));
break;
}
projJournalTrans.Voucher = numSeq.voucher();
numSeq.used();
projJournalTrans.TransDate = Today();
projJournalTrans.ProjTransDate = str2date(projtransdate, 321);

projJournalTrans.JournalId = jourNum;
projJournalTrans.LineNum = str2num(lineNum);

projJournalTrans.CostPrice = str2num(costprice);
projJournalTrans.SalesPrice = str2num(salesprice);
projJournalTrans.EmplId = empl;
projJournalTrans.LinePropertyId = lineprop;
projJournalTrans.CategoryId = category;
projJournalTrans.txt = transtxt;
projJournalTrans.CurrencyId = salescurrency;

projJournalTrans.Dimension[1] = businessline;
projJournalTrans.Dimension[2] = costcenter;

projJournalTrans.ProjId = projectid;
projJournalTrans.Dimension[4] = projectid;
projJournalTrans.Qty = str2num(qty);

projJournalTrans.insert();

ttscommit;
[projtransdate, salescurrency, costprice, projectid, salesprice ,qty, transtxt, businessline, costcenter, lineprop, category, empl] = io.read();
}

}

ttsbegin;
select firstonly forupdate projJournalTable where projJournalTable.JournalId == jourNum;
projJournalTable.NumOfLines = projJournalTrans.numOfLines();
projJournalTable.ProjQty = TotalHours;
projJournalTable.update();
ttscommit;

info(strFmt(“import complete: Imported %1 records. New Journal %2 created”, (n-1), journum));
element.close();
}

`

use this code…

static void createProjectJournal(Args _args)///kranthi
{
ProjJournalTable projJournalTable;
ProjJournalTrans projJournalTrans;
AxProjJournalTrans axProjJournalTrans;
int j;
;
ttsBegin;
projJournalTable.JournalNameId = “Hours”;
projJournalTable.initFromProjJournalName(ProjJournalName::Find(projJournalTable.JournalNameId));
projJournalTable.insert();
j++;
axProjJournalTrans = new AxProjJournalTrans();
axProjJournalTrans.parmJournalId(projJournalTable.JournalId);
axProjJournalTrans.parmTransDate(systemdateget());
axProjJournalTrans.parmProjTransDate(systemdateget());
axProjJournalTrans.parmLineNum(j);
axProjJournalTrans.parmCategoryId(“Design”);/// for category
axProjJournalTrans.parmCostPrice(100);/// for Cost Price
axProjJournalTrans.parmEmplId(‘1000’);/// for Employee
axProjJournalTrans.parmProjId(‘10007’);/// for Project Id
axProjJournalTrans.parmSalesPrice(110);/// for Sales Price
axProjJournalTrans.parmQty(150);/// for Qty(Number of hours)
axProjJournalTrans.save();
ttsCommit;
}

Hi

There is no Class named AxProjJournalTrans in AX 4.0, but perhpas you are working in 5.0? or did you misstype??

I am not sure about the existance of this class in 4.0- i have written this in 5.0.

Try to use it

ttsBegin;
projJournalTable.JournalNameId = “Hours”;
projJournalTable.initFromProjJournalName(ProjJournalName::Find(projJournalTable.JournalNameId));
projJournalTable.insert();

projJournalTrans.initFromProjJournalTable(projJournalTable);
projJournalTrans.initFromProjJournalTrans(projJournalTrans);
numSeq = NumberSeq::newGetVoucherFromCode(projJournalTable.VoucherSeqId);
projJournalTrans.Voucher = numSeq.voucher();
numSeq.used();
projJournalTrans.TransDate = Today();
projJournalTrans.ProjTransDate = Today();
projJournalTrans.LineNum = str2num(lineNum);
projJournalTrans.CostPrice = 100;
projJournalTrans.SalesPrice = 120;
projJournalTrans.EmplId = “1000”;
projJournalTrans.initFromEmplTable();
projJournalTrans.CategoryId = “Design”;
projJournalTrans.txt = “Design”;
projJournalTrans.ProjId = “10007”;
projJournalTrans.initFromProjTable(ProjTable::find( projJournalTrans.ProjId));
projJournalTrans.Qty = 15;
projJournalTrans.insert();
ttscommit;

Hi Kranthi,

well this code does bascially the same as my code, would you care to elboarate what the difference is and how that will solve the inital error (Record already excist)?

I think the issue lies in how I assign a new number sequence for the voucher, but I could be wrong. Any tips would be appriciated.

Hi Kranthi,

I just fouund the difference in your code, it is the InitFromProjJournal… part that does the trick

Now it works. Thanks!!