I am using sql server option. I have a table where i import lines from an xml file. There is a codeunit that needs to read lines from this, split each line into Header & Lines, find Codes & Items from descriptions that came from xml.
Its quite crude right now & looks like:
OnRun()
XMLSales.SETCURRENTKEY("Line No.");
IF XMLSales.FINDFIRST THEN
REPEAT
MESSAGE('Handling XML Line No. %1', XMLSales."Line No.");
//find if a header already exists
SalesHeader.SETRANGE(SalesHeader."Order Date", (WORKDATE));
SalesHeader.SETFILTER(SalesHeader."Document Type", '1');
SalesHeader.SETFILTER(SalesHeader."Sell-to Customer No.", '10005');
IF NOT SalesHeader.FIND('-') THEN
CreateHeader();
AssignSalesLineNo();
SalesLine."Document Type" := SalesHeader."Document Type";
SalesLine."Document No." := SalesHeader."No.";
SalesLine.INSERT(TRUE);
//Get Corresponding ltem no. for description from XML; write rest of data to record
SalesLine.VALIDATE(SalesLine."No.", ItemRec."No.");
SalesLine.VALIDATE(SalesLine.Quantity, XMLSales.Quantity);
SalesLine.VALIDATE(SalesLine."Qty. to Ship", XMLSales.Quantity);
SalesLine.VALIDATE(SalesLine."Line Amount", XMLSales.Amount);
SalesLine.VALIDATE(SalesLine."Amount Including VAT", XMLSales.TaxedAmount);
SalesLine.MODIFY;
XMLSales.DELETE;
UNTIL XMLSales.NEXT = 0;
It works great when it creates first header and adds lines, when it comes to creating second header, it fails, because the first header is not committed in tables; so the code ends up generating same header number and errors out with 'Header No. # already exists'
What could be better way to do this?
Thanks,
Amol
0
Comments
SalesHeader."no." := '';
Salesheader.insert(true);
but I'm not psychic.
Main problem is somewhere in CreateHeader but because you haven't included the code... :-k
Little off-topic:
My knife is opening each time I see something like :whistle:
Please, use Option data type as Option data type...
Use SetRange(Field,Value) or SetFilter(Field,String,Value,Value,Value...) where string is like '%1|%2..%3' never (with exceptions of course like filters saved in fields) SetFilter(Field,Value)
Next one: FINDFIRST and REPEAT UNTIL? I recommend to read some manuals and posts about how to use FINDFIRST and FINDSET correctly, this is something what each developer MUST know...
Another one: Big problem... you delete record on which you are and then you want from NAV to go to next record. But it is hard to say what is next when you are on record which doesn't exists...
Why not to use something like:
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
RIS Plus, LLC
MVP - Business Apps
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
Try NEVER to use constants like the '1' and '10005' here. Put them in a settingstable somewhere so they can be changed easily later, if needed.
For efficiency, this should be replaced by IF SalesHeader.IsEmpty THEN