XMLPort Import Sales Order / Sales Lines

WhoAmIClay
Member Posts: 3
Hey Guys,
i'm a newbie at C/AL Coding, so i hope, you guys can help me
I have a XML-File, which should be imported to a sales order.
I already got, that i have a sales header.
This Code works finde
Now i have to add the sales lines to the same Sales Order. That's what i've got so far, but the error message said, that there ist no sales header with number XYZ.
If i comment out the whole code, the header is there with the right customer.
Do i have to write the sales line code into the first code?
Cheers
Clay
i'm a newbie at C/AL Coding, so i hope, you guys can help me

I have a XML-File, which should be imported to a sales order.
I already got, that i have a sales header.
This Code works finde
IF NOT "Sales Header".GET("Sales Header"."Document Type"::Order, "Sales Header"."No.") THEN BEGIN
"Sales Header".INIT;
"Sales Header"."Document Type" := "Sales Header"."Document Type"::Order;
"Sales Header"."No." := "Sales Header"."No.";
Customer.SETRANGE("No.", customer_nr_from_xml);
IF Customer.FIND('-') THEN BEGIN
"Sales Header"."Sell-to Customer No." := Customer."No.";
"Sales Header".VALIDATE("Sell-to Customer No.", Customer."No.");
END;
END;
Now i have to add the sales lines to the same Sales Order. That's what i've got so far, but the error message said, that there ist no sales header with number XYZ.
If i comment out the whole code, the header is there with the right customer.
IF NOT "Sales Line".GET("Sales Line"."Document Type"::Order, "Sales Line"."No.") THEN BEGIN
"Sales Line".INIT;
IF "Sales Line".FIND('+') THEN BEGIN
"Sales Line"."Document Type" := "Sales Line"."Document Type"::Order;
"Sales Line"."Document No." := "Sales Line"."Document No."
END;
Item.SETRANGE("No. 2", katalognr);
IF Item.FIND('-') THEN BEGIN
"Sales Line"."Document Type" := "Sales Header"."Document Type"::Order;
"Sales Line"."Document No." := "Sales Header"."No.";
"Sales Line".Type := "Sales Line".Type::Item;
"Sales Line"."No." := Item."No.";
"Sales Line"."Line No." := positionnr;
"Sales Line".VALIDATE("No.");
END;
positionnr := positionnr + 10000;
END;
Do i have to write the sales line code into the first code?
Cheers
Clay
0
Comments
-
What is your NAV version?United Kingdom0
-
Developer101 wrote: »What is your NAV version?
NAV2017 / Version 10.0.165850 -
Sorry you mentioned CAL in your questions, I missed it. Version does not matter here.
How have you structured your XML Port? Did you structure Sales Header as parent node and Sales Lines as child node? Which trigger are your placing your codes ?United Kingdom0 -
Developer101 wrote: »Sorry you mentioned CAL in your questions, I missed it. Version does not matter here.
How have you structured your XML Port? Did you structure Sales Header as parent node and Sales Lines as child node? Which trigger are your placing your codes ?
No Problem!
In a late Night Session i was succesful, so my import is working fine now.
Nevertheless i think, my code is not as good as it should be...
Function "CreateSalesLine() : Code [10]CreateSalesHeader; "Sales Line".INIT; Item.SETRANGE("No. 2", katalognr); IF Item.FIND('-') THEN BEGIN "Sales Line"."Document Type":= "Sales Header"."Document Type"::Order; "Sales Line"."Document No." := "Sales Header"."No."; "Sales Line".Type := "Sales Line".Type::Item; "Sales Line"."No." := Item."No."; "Sales Line"."Line No." := positionnr; "Sales Line".RESET; "Sales Line".SETFILTER("Quantity",menge); txt := "Sales Line".GETFILTER("Quantity"); EVALUATE(dec,txt); "Sales Line".Quantity := dec; "Sales Line".VALIDATE("No."); END; "Sales Line".INSERT(TRUE); positionnr := positionnr + 10000;
Function "CreateSalesHeader() : Code[10]IF NOT "Sales Header".GET("Sales Header"."Document Type"::Order, "Sales Header"."No.") THEN BEGIN "Sales Header"."Document Type" := "Sales Header"."Document Type"::Order; "Sales Header"."No." := "Sales Header"."No."; Customer.SETRANGE("No.", kundennummer_beim_haendler); IF Customer.FIND('-') THEN BEGIN "Sales Header"."Sell-to Customer No." := Customer."No."; "Sales Header".VALIDATE("Sell-to Customer No.", Customer."No."); END; END; CASE positionnr OF 10000: "Sales Header".INSERT(TRUE); ELSE MESSAGE('ok'); END;
0 -
A lot of improvements are needed.
Using temporary instances of sales headers and sales lines could have been useful.
Validates are missing in sales lines
Not sure about the use of "Sales Line".SETFILTER("Quantity",menge);
Sales Line code could have been put into a function as well.United Kingdom0 -
Hmm, I can understand it works... but quite a bit of unnecessary code and also wrongly put IF ELSE and inserts.
As Developer101 said, a lot of improvements are needed.
For example, on your sales line if the item does not exists, you still create a Sales Line, but it's not linked to anything. If that happens twice, you will get errors and your import no longer works and without a way for users to fix it.
I'm certain I can still learn a thing or two, but I'd start by making it like this:... CreateSalesHeader(); CreateSalesLine();
IF NOT HeaderExists THEN BEGIN IF NOT "Sales Header".GET("Sales Header"."Document Type"::Order, "Sales Header"."No.") THEN BEGIN "Sales Header"."Document Type" := "Sales Header"."Document Type"::Order; "Sales Header"."No." := "Sales Header"."No."; Customer.SETRANGE("No.", kundennummer_beim_haendler); IF NOT Customer.ISEMPTY() THEN "Sales Header".VALIDATE("Sell-to Customer No.", kundennummer_beim_haendler); "Sales Header".INSERT(TRUE); END; HeaderExists := TRUE; END;
positionnr += 10000; "Sales Line".INIT; "Sales Line"."Document Type":= "Sales Header"."Document Type"::Order; "Sales Line"."Document No." := "Sales Header"."No."; "Sales Line"."Line No." := positionnr; "Sales Line".Type := "Sales Line".Type::Item; Item.SETRANGE("No. 2", katalognr); IF Item.FINDFIRST() THEN "Sales Line".VALIDATE("No.", Item."No." ); IF EVALUATE(dec, menge) THEN "Sales Line".VALIDATE(Quantity, dec); "Sales Line".INSERT(TRUE);
0
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