Error creating purchase orders

Hi everyone,

I’m trying to make an easy development to create purchase orders with the aim of having covered the safety stock of the items into a location. The code is like this:

lPedidoGenerado := FALSE;
lCodPedido := '';
lLineaPedido := 0;
lItem.SETCURRENTKEY("Vendor No.");
lItem.SETFILTER("Safety Stock Quantity",'>0');
lItem.SETRANGE(Type, lItem.Type::Inventory);
   IF lVendor.GET(lItem."Vendor No.") THEN
    IF lVendor."Location Code" = 'ALM01' THEN BEGIN 
      IF lVendorCode <> lItem."Vendor No." THEN BEGIN
        lVendorCode := lItem."Vendor No."; 
        lPedidoGenerado := FALSE;
      lItem.CALCFIELDS("Qty. on Purch. Order");
      lItem.CALCFIELDS("Qty. on Sales Order");
      //lQtyOnPurchase := lItem."Qty. on Purch. Order";
      lDisponibilidad := lItem.Availability;// + lQtyOnPurchase;
      lStockSeg := lItem."Safety Stock Quantity";
      IF lDisponibilidad < lStockSeg THEN BEGIN
        IF NOT lPedidoGenerado THEN BEGIN
          //crea la cabecera del  pedido y añade la primera linea
          lPedidoGenerado := TRUE;
          lPurchaseHeader."Document Type" := lPurchaseHeader."Document Type"::Order;
          //lPurchaseHeader."Buy-from Vendor No." := lVendorCode;
          lPurchaseHeader.VALIDATE("Buy-from Vendor No.",lVendorCode);
          lPurchaseHeader."Order Date" := WORKDATE;
          lPurchaseHeader."Vendor Invoice No." := 'AITOR';

When a new product is has a different vendor, I cerate a new purchase order. But I can see that INIT and RESET aren’t doing anything. The INSERT is not returning any error, but in the VALIDATE of the “Buy-from vendor No.”, I get this error:


“There is already exist a pruchase header of type ordr and code 002507”

002507 is the first purchase order created when I run the function. Any hint?

Thnak you very much


I think RESET and INIT functions do their job here, but that’s not what you need. RESET removes all filters from a record variable, but you don’t apply any filters (at least, not in the snippet shown here). So it seems, you don’t need reset at all.

Now, INIT. It initializes the record, but does not affect primary key and timestamp fields, as described in help:–record-

What you need in this case is function CLEAR

INSERT does not return any error, because it does not actually call the SQL insert query the moment you invoke this statement, considering you have buffered insert enabled. And from the symptoms described, it looks like it is enabled.

So, to sum up: it’s not a bug, but completely expected outcome. Just call CLEAR instead of INIT.

That’s right, I’ve found the solution after posting this message. CLEAR was the solution, thank you!!