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;
Answers
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
RIS Plus, LLC