Validation of on same item no. used on Sales Order

liizz
liizz Member Posts: 125
Hello,

I want to add a validation on the Sales line on the Sales Order form whereby it checks that the same item no. is not being used on lines.

No.-OnValidate
SalesLine.SETRANGE(No.,No.);
IF SalesLine.FINDFIRST THEN
ERROR('Same item no. cannot be used more than once');

But, even after picking a new item, it is passing into the above codes. I think the item no. should be immediately cleared out after prompting the error message.

Your help is very appreciated..

Liizz

Comments

  • Luc_VanDyck
    Luc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Please show us the real C/AL code. This one won't compile.

    The Item No. is stored in the field "Item No." of the Sales Line table, not the field "No.".

    EDIT: You should also filter on Document Type and Document No.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • David_Singleton
    David_Singleton Member Posts: 5,479
    The Item No. is stored in the field "Item No." of the Sales Line table, not the field "No.".

    are you sure? :mrgreen:
    David Singleton
  • Luc_VanDyck
    Luc_VanDyck Member, Moderator, Administrator Posts: 3,633
    are you sure? :mrgreen:
    Nope, don't know what happened to me
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • liizz
    liizz Member Posts: 125
    Its No. in the Sales Line table which lookup in the Item list.

    Document Type Document No. Line No. Type No.
    Order 1001 10000 Item 1928-W
  • liizz
    liizz Member Posts: 125
    The Item No. is stored in the field "Item No." of the Sales Line table, not the field "No.".

    Its ok about it.

    Is there a possible way to clear out the field automatically after the error message.

    Liizz
  • vijay_g
    vijay_g Member Posts: 884
    What do you need to clear this field it should work fine without clear the field. where you have written the code to check duplicate item?
  • liizz
    liizz Member Posts: 125
    I have written the code on this trigger:No.-OnValidate.

    It is checking for duplicate items but the Item No. should be cleared after the error message. Instead of deleting the item no. manually.


    Thanks
    Liizz
  • Savatage
    Savatage Member Posts: 7,142
    We do it like this:
    No. - OnValidate()
    //Item can only show up once per SO
    SalesLineChk.SETCURRENTKEY("Document Type", "Document No.");
    SalesLineChk.SETRANGE("Document Type", "Document Type");
    SalesLineChk.SETRANGE("Document No.", "Document No.");
    SalesLineChk.SETRANGE(Type,SalesLineChk.Type::Item);
    Existed := FALSE;
    //Add Setup Boolean to turn on and off service
    SalesSetup.GET;
    IF SalesSetup."Enable Duplicate Item Dialog" THEN BEGIN
    //check if already exists not on returns or credits
    IF NOT ("Document Type" IN ["Document Type"::"Return Order","Document Type"::"Credit Memo"])
    THEN BEGIN
    IF SalesLineChk.FIND('-') THEN REPEAT
        IF SalesLineChk."No." = "No." THEN
          IF NOT CONFIRM ('Item Already on order. Do you want to add item again?') THEN BEGIN
              MESSAGE('Item disregarded.');
              "No." := '';
              SalesLineChk.NEXT := 0;
            END ELSE
            Existed := TRUE;
      UNTIL (SalesLineChk.NEXT = 0) OR Existed;
    END;
    END;
    

    C/AL Locals: (Variables)
    SalesLineChk - Record - Sales Line
    Existed - Boolean
  • DenSter
    DenSter Member Posts: 8,307
    liizz wrote:
    I have written the code on this trigger:No.-OnValidate.
    The OnValidate trigger of the form or the table? This needs to be in the TABLE.
  • Savatage
    Savatage Member Posts: 7,142
    On my code it is on the No. - OnValidate() of the sales line table.

    We've made it so it only looks at ITEM types incase you need to add more than one comment line or g/l.

    Also it allows duplicates on credit memo's & return orders because we use two locations(Active & Damaged)
    so if a customer returns 10 pieces... 5 good & 5 bad we enter the item 2x
    item 123 - pcs 5 - location damaged
    item 123 - pcs 5 - location active

    you have to make sure your not affecting other types of orders, that the sales line table handles, that you like they way they are working.