Insert record in loop not find in the next iteration (Dynamics BC170)

navidnavid Member Posts: 5
edited 2020-12-22 in NAV Three Tier
I am trying to write this code
        ItemDistributions.SetRange("DSM Code", ordPlnHeader_rec.DMS);
        ItemDistributions.SetRange("Item No", itemNo);
        ItemDistributions.SetRange("Supplier Type", ItemDistributions."Supplier Type"::"Sole Supplier");
        if ItemDistributions.FindFirst() then begin
            repeat           
                    PurchaseHeader.Reset();
                    PurchaseHeader.SetRange("Buy-from Vendor No.", ItemDistributions."Vendor No");
                    PurchaseHeader.SetRange("Document Date", Today);
                    if not PurchaseHeader.FindFirst() then begin

                        PurchSetup.Get();
                        PurchaseHeader.Init();
                        PurchaseHeader.validate("Document Type", PurchaseHeader."Document Type"::Order);
                        PurchaseHeader.validate("No.", '');
                        PurchaseHeader.validate("Buy-from Vendor No.", ItemDistributions."Vendor No");
                        PurchaseHeader.Validate("Document Date", Today);
                        PurchaseHeader.Insert(true);
                    end;
                    
                    //Insert the lines...

         Until ItemDistributions.next = 0;


basically if one header is inserted for a vendor for that day I want to use that header to insert more line. and so on ... But after inserting one header when the record repeats it does not find the inserted record for same vendor. What did I wrong here can any one suggest me? Thanks in advance.

Answers

  • satbirsatbir Member Posts: 33
    You have ThisWeek variable for document date and filter is Today.
  • navidnavid Member Posts: 5
    Thanks for your answer. but this thisweek is same as today (I have corrected in the question). Even if I use today in both place same thing is happening.
  • DenSterDenSter Member Posts: 8,307
    edited 2020-12-22
    The Document Date is set to WorkDate during OnInsert. Follow the code and see if you can find it.

    Also, FindFirst only gets one record. It works because it's a common mistake, but when the code gets to the 'Next' it has to go back to the database for the next record. When you need to loop through records, you should use FindSet, that gets the whole set of records in one database trip
  • navidnavid Member Posts: 5
    Thanks a lot for the answer and nice tips. I did not know about this.
Sign In or Register to comment.