XMLPort Import Sales Order / Sales Lines

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
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

Comments

  • Developer101Developer101 Member Posts: 476
    What is your NAV version?
    United Kingdom
  • WhoAmIClayWhoAmIClay Member Posts: 3
    What is your NAV version?

    NAV2017 / Version 10.0.16585
  • Developer101Developer101 Member Posts: 476
    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 Kingdom
  • WhoAmIClayWhoAmIClay Member Posts: 3
    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;
    
  • Developer101Developer101 Member Posts: 476
    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 Kingdom
  • ResolusResolus Member Posts: 40
    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);
    
Sign In or Register to comment.