Problem with a code on codeunit 81

DarkHorseDarkHorse Member Posts: 389
Hello, I've ask for this code before but now I've another problem. I've this code on the codeunit 81.

salesline.RESET;
salesline.SETRANGE("Document Type", SalesHeader."Document Type");
salesline.SETRANGE("Document No.", SalesHeader."No.");
salesline.SETRANGE("Sell-to Customer No.", SalesHeader."Sell-to Customer No.");
reserved.RESET;
IF reserved.GET(salesline."Sell-to Customer No.")THEN;
IF salesline.FIND('-')THEN BEGIN;
IF reserved.FIND('-') THEN
REPEAT
reserved.CALCFIELDS("Item quantity");
cant:=reserved."Item quantity"-reserved."Quantity to reserved";
IF (salesline."No."=reserved."Item") AND (reserved."Nº customer"<>salesline."Sell-to Customer No.")
AND (salesline.Quantity>cant) AND (reserved.Reserved=TRUE) AND (reserved."Cancel reserved"=FALSE)
THEN ERROR('You can't register, there is a reserved quantity');
UNTIL reserved.NEXT=0;

salesline is sales line table
reserved is a table that I've created
cant is a varible

This code, on register a sales quote, goes to the "reserved" table and look for if the item from the sales quote has a reserved quantity. If there is a reserved quantity for another customer and there aren't enough quantity it shows the message "'You can't register, there is a reserved quantity" and doesn't let to register.
This code runs well, but if only you put a reserved item on the first line from the sales quote, I mean, if you put an item on the first position from the sales quote that isn't reserved and followint put a second item that is reserved and you go to register the reserved item, it let registed; it seems as doesn't check all the sales lines and only remain on first position. How can I look for all the positions? what is mising on my code?.
Thanks in advance.

Comments

  • kapamaroukapamarou Member Posts: 1,152
    From a first look I can spot the following:

    IF salesline.FIND('-')THEN BEGIN;

    This will try to find the first line but will not loop the lines since the ";" is after begin.

    salesline.RESET;
    salesline.SETRANGE("Document Type", SalesHeader."Document Type");
    salesline.SETRANGE("Document No.", SalesHeader."No.");
    salesline.SETRANGE("Sell-to Customer No.", SalesHeader."Sell-to Customer No.");
    reserved.RESET;
    IF reserved.GET(salesline."Sell-to Customer No.")THEN;

    You have applied filters on the sales line after a reset , but you don't use a FIND so the sales line fields will most probably be empty.


    You should change it to something like this:

    The old
    IF salesline.FIND('-')THEN BEGIN;
    

    should become:
    IF salesline.FIND('-')THEN BEGIN
      REPEAT
        //In here you should apply filters for reserved and search for lines...
      UNTIL salesline.NEXT = 0;
    END;
    
  • DarkHorseDarkHorse Member Posts: 389
    Thanks for reply. I've tested the code like this:

    salesline.RESET;
    salesline.SETRANGE("Document Type", SalesHeader."Document Type");
    salesline.SETRANGE("Document No.", SalesHeader."No.");
    salesline.SETRANGE("Sell-to Customer No.", SalesHeader."Sell-to Customer No.");
    reserva.RESET;
    IF reserva.GET(salesline."Sell-to Customer No.")THEN;
    IF salesline.FIND('-')THEN BEGIN
    REPEAT
    //IF reserva.FIND('-') THEN;
    //REPEAT
    reserva.CALCFIELDS("Existencias producto");
    cant:=reserva."Existencias producto"-reserva."Cantidad a reservar";
    IF (salesline."No."=reserva."Cód. producto") AND (reserva."Nº cliente"<>salesline."Sell-to Customer No.")
    AND (salesline.Quantity>cant) AND (reserva.Reservar=TRUE) AND (reserva."Anular reserva"=FALSE)
    THEN ERROR('No puede registrar, hay una cantidad reservada. Contacte con la secretaria de ventas');
    UNTIL salesline.NEXT=0;

    But it doesn' work yet. Any idea more, please?

    Thanks for help.
  • kapamaroukapamarou Member Posts: 1,152
    How do you link salesline with reserve?

    You code should be like this:
    //IF reserva.GET(salesline."Sell-to Customer No.")THEN; //<-- Remove this 
    IF salesline.FIND('-')THEN BEGIN
    REPEAT
      //IF reserva.GET("Your PK FIELDS") // <- If you know the P.K. ELSE
      reserva.RESET; //<- Add Your Filters
      reserva.SETRANGE(reserva.field1,salesline.LINKFIELD1); //<- Add Your Filters
      reserva.SETRANGE(reserva.field2,salesline.LINKFIELD2); //<- Add Your Filters
      IF reserva.FINDSET then REPEAT //If there is a linked reserva then ...
        reserva.CALCFIELDS("Existencias producto");
        cant:=reserva."Existencias producto"-reserva."Cantidad a reservar";
        IF (salesline."No."=reserva."Cód. producto") AND (reserva."Nº cliente"<>salesline."Sell-to        Customer No.")
        AND (salesline.Quantity>cant) AND (reserva.Reservar=TRUE) AND (reserva."Anular    reserva"=FALSE)
       THEN ERROR('No puede registrar, hay una cantidad reservada. Contacte con la secretaria de    ventas');
       UNTIL reserva.NEXT = 0;
    UNTIL salesline.NEXT=0;
    
  • DarkHorseDarkHorse Member Posts: 389
    Thanks for your help. I've leave the code finally like this:
    IF salesline.FIND('-')THEN BEGIN
    REPEAT
      IF reserva.GET(salesline."Sell-to Customer No.")THEN;
      reserva.RESET;
      IF reserva.FIND('-') THEN REPEAT
        reserva.CALCFIELDS("Existencias producto");
        cant:=reserva."Existencias producto"-reserva."Cantidad a reservar";
        IF (salesline."No."=reserva."Cód. producto") AND (reserva."Nº cliente"<>salesline."Sell-to Customer No.")
        AND (salesline.Quantity>cant) AND (reserva.Reservar=TRUE) AND (reserva."Anular reserva"=FALSE)
       THEN ERROR('No puede registrar, hay una cantidad reservada. Contacte con la secretaria de ventas');
       UNTIL reserva.NEXT = 0;
    UNTIL salesline.NEXT=0;
    

    Now runs well =D> !!, but only one thing. If there are two lines, one reserved and other don't reserved and you want to register the don't reserved item, it doesn't let you do it, it says that there is a reserved quantity, it seems like as the code works by the whole quote doesn't by lines. Sorry if I don't explain well myself. Do you know what can happend, if I don't bother you so much :oops: ?
    Thanks for your help.
  • kapamaroukapamarou Member Posts: 1,152
    There are a couple of problems here..

    You will need to study carefully the documentation.

    So a couple of hints:

    Do you know GET vs FIND?

    GET works ONLY on the primary key like:
    SalesHeader.GET(SalesHeader."Document Type",'SomeDocNo');
    

    GET is NOT affected by filters.

    If you do a GET and no filters are applied, then if you do a FIND / REPEAT / UNTIL then you will loop the whole table.

    So correct the following part:
    IF reserva.GET(salesline."Sell-to Customer No.")THEN;
      reserva.RESET;
      IF reserva.FIND('-') THEN REPEAT
    


    What is the primary key of reserva?
    reserva.GET(salesline."Sell-to Customer No.") means that the Primary key for the table is the Customer No. So for any customer you can have only 1 reservation no matter how many documents / lines exist.

    You should change the table to have a primary key with at least the same primary key fields as the sales line table (I suggest you take a look at the standard Navision Reservation Entry table to get an idea...). That way you'll be able to add reservations for each line of each type of document of each customer.
  • DarkHorseDarkHorse Member Posts: 389
    Thanks for reply. The primary key from reserva table is:

    Cód. producto,Nº cliente,Cantidad a reservar,Reservar,Anular reserva

    Translation: Item, Customer N, Quatity to reserve, Reserved, Cancel reservation

    But I can't create a primary key with the fields from sales line table key because the table hasn't these fields. The first primary key from sales line table is:

    Document Type,Document No.,Line No.

    I don't know what more can I do.
    Thanks for your time and help.
  • kapamaroukapamarou Member Posts: 1,152
    What I make of this is the following:

    This tables keeps track of the reservation per Item per Customer.

    So you can do:
    IF salesLine.Type = SalesLine.Type::Item THEN BEGIN
    Reserve.SETRANGE("Cód. producto",SalesLine."No.");
    Reserve.SETRANGE("Nº cliente",SalesLine."Sell-To Customer No.");
    IF reserve.FIND('-') THEN BEGIN
    END;
    END;

    But I thought that the table was created by you. So you could add the fields, change the primary key and change the code so that it stores that info...

    The way it is now you can get the reserved quantity per Item per Customer, but not per Order neither per Line...
  • DarkHorseDarkHorse Member Posts: 389
    Thanks for help, but I think is impossible to change that because the primary key from my table has his function on a sereval forms, it should be very hard to me change it.
    Thank you very much for your help and time.
  • kapamaroukapamarou Member Posts: 1,152
    You're welcome. You could continue with the table as it is.
    But I really suggest you take a look at table 337 which is the standard navision reservation table. At a minimum it will give you ideas... :D
Sign In or Register to comment.