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;
lVendorCode:='';
lItem.RESET;
lItem.SETCURRENTKEY("Vendor No.");
lItem.SETFILTER("Safety Stock Quantity",'>0');
lItem.SETRANGE(Blocked,FALSE);
lItem.SETRANGE(Type, lItem.Type::Inventory);
IF lItem.FINDSET THEN REPEAT 
   lVendor.RESET;
   IF lVendor.GET(lItem."Vendor No.") THEN
    IF lVendor."Location Code" = 'ALM01' THEN BEGIN 
      IF lVendorCode <> lItem."Vendor No." THEN BEGIN
        COMMIT;
        lVendorCode := lItem."Vendor No."; 
        lPedidoGenerado := FALSE;
        lPurchaseHeader.INIT;
        lPurchaseHeader.RESET;
      END;           
      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.INSERT(TRUE);
          //lPurchaseHeader."Buy-from Vendor No." := lVendorCode;
          lPurchaseHeader.VALIDATE("Buy-from Vendor No.",lVendorCode);
          lPurchaseHeader."Order Date" := WORKDATE;
          lPurchaseHeader."Vendor Invoice No." := 'AITOR';
          lPurchaseHeader.MODIFY;
          ......

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

Parents Reply Children
No Data
Related
Recommended