Programming Question

b2amol
Member Posts: 64
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
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
-
Probably the error is in the CreateHeader function, which you have not shown. If I was psychic I would say you are missing a bit of code like
SalesHeader."no." := '';
Salesheader.insert(true);
but I'm not psychic.David Singleton0 -
If you want to do
SalesLine.VALIDATE(SalesLine."No.", ItemRec."No.");
you need to select correct Type firstSalesLine.VALIDATE(Type, SalesLine.Type::Item);
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 likeSalesHeader.SETFILTER(SalesHeader."Document Type", '1');
:whistle:
Please, use Option data type as Option data type...SalesHeader.SETRANGE("Document Type", SalesHeader."Document Type"::Order); or SalesHeader.SETFILTER("Document Type", '%1',SalesHeader."Document Type"::Order);
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:XMLSales.DELETE; UNTIL XMLSales.NEXT = 0;
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:UNTIL XMLSales.NEXT = 0; XMLSales.DELETEALL;
0 -
I think Kamil is psychic, he understood that he need to put together an entire lesson plan0
-
DenSter wrote:I think Kamil is psychic, he understood that he need to put together an entire lesson plan0
-
Continuing on Kine list of suggestions :
SalesHeader.SETFILTER(SalesHeader."Document Type", '1'); SalesHeader.SETFILTER(SalesHeader."Sell-to Customer No.", '10005');
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.IF NOT SalesHeader.FIND('-') THEN
For efficiency, this should be replaced by IF SalesHeader.IsEmpty THEN0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions