Programming Question

b2amolb2amol 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

Comments

  • David_SingletonDavid_Singleton Member Posts: 5,479
    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 Singleton
  • kinekine Member Posts: 12,562
    If you want to do
    SalesLine.VALIDATE(SalesLine."No.", ItemRec."No.");
    
    you need to select correct Type first
    SalesLine.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 like
    SalesHeader.SETFILTER(SalesHeader."Document Type", '1');
    
    :mrgreen: :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;
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DenSterDenSter Member Posts: 8,304
    I think Kamil is psychic, he understood that he need to put together an entire lesson plan :mrgreen:
  • kinekine Member Posts: 12,562
    DenSter wrote:
    I think Kamil is psychic, he understood that he need to put together an entire lesson plan :mrgreen:
    Yes, I will like that, and I will teach every NAV developer on the word... and they will pay big money for it... yeah.... :mrgreen:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • MBergerMBerger Member Posts: 413
    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 THEN
Sign In or Register to comment.