how to ban the same item (Article ) on two lines orders

Hi,
how to ban the same item (Article ) on two lines orders
Thanks

Answers

  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    You cannot - you need a customisation to achieve this.

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • vivek4121vivek4121 Member Posts: 165
    For a two lines order you can add below code on the "OnInsert" Trigger of Purchase Line / Sales Line Table "
    IF "Line No." > 0 THEN
      IF xRec."No." = Rec."No." THEN
        ERROR ('You cannot add same Item No. on 2 lines');
    
  • pn7pn7 Member Posts: 17
    vivek4121 wrote: »
    For a two lines order you can add below code on the "OnInsert" Trigger of Purchase Line / Sales Line Table "
    IF "Line No." > 0 THEN
      IF xRec."No." = Rec."No." THEN
        ERROR ('You cannot add same Item No. on 2 lines');
    

    I sense that check must cover every line so:
    IF "Line No." > 0 THEN BEGIN
      SalesLine2.RESET;
      SalesLine2.SETFILTER("No.", Rec."No.");
      IF SalesLine2.FINDSET THEN
        REPEAT
          IF SalesLine2."No." = Rec."No." THEN
            ERROR ('You cannot add same Item No. on 2 lines');
        UNTIL SalesLine2.NEXT = 0;
    END;
    
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    All good, except this is almost an anti-pattern.

    It should be more like this:
    IF (Type = TYpe::Item) AND ("No." <> '') THEN BEGIN
      SalesLine2.RESET;
      SalesLine2.SETRANGE("Document No.", "Document No.");
      SalesLine2.SETFILTER("Line No.", '<>%1', "Line No.");
      SalesLine2.SETRANGE(Type, Type);
      SalesLine2.SETRANGE("No.", "No.");
      IF NOT SalesLine2.ISEMPTY THEN
        ERROR('...');
    END;
    
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • RockWithNAVRockWithNAV Member Posts: 1,139
    I believe there are many ways to handle this, depends on how and when you wanted this to be implemented and executed.

    Better way is to handle it right way on the No column as soon as User enters the same Item No.
  • vivek4121vivek4121 Member Posts: 165
    Hi Pn7,
    Checking the same for every line the code provided by you will work.
    Thanks
  • beharbehar Member Posts: 3

    No. - OnValidate()
    SL2.RESET;
    SL2.SETRANGE("Document Type","Document Type");
    SL2.SETRANGE("Document No.","Document No.");
    SL2.SETRANGE("No.","No.");
    IF SL2.FIND('-') THEN
    ERROR('You cannot add same Item No. on 2 lines');
  • vivek4121vivek4121 Member Posts: 165
    Hi All,

    Just an update, with the below code added on "OnValidate Trigger" of No. Field we cannot create even the second purchase Order with same Item if the first Purchase order is not fully received and invoiced (and moved to achieve)
    IF (Type = Type::Item) AND ("No." <> '') THEN BEGIN
      PurchLine2.RESET;
      PurchLine2.SETRANGE("No.", Rec."No.");
      IF PurchLine2.FINDSET THEN 
        REPEAT
          IF PurchLine2."No." = Rec."No." THEN
              ERROR ('You cannot add same Item No. on 2 lines');
          UNTIL PurchLine2.NEXT = 0;
          END;
    

    Seems not feasible for all scenarios.

    Thanks in advance.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2017-11-15
    That is unfortunately a fate of most restrictions - they don't do very well in various scenarios, and require constant refining and refining. Adn quite often missed consequence is that users slowly stop using their brain - after all system will let them, or will not. And if they do some stupid thing the excuse is - but the system let me....

    Rather than spending time on inventing various restrictions, and then on fine tuning, maintaining and adapting them to constant business changes, spend this time with your users. But spend it wisely - do not tell them what to do and what not, end of - explain the process and tell WHY doing something this or that way is important for the business, and the impact of not doing thing as business requires.

    If you like spending your time on coding spend it rather on writting a report highlighting cases of non-compliance, so you can re-train specific users should they make a mistake

    My 0.02£

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AKAK Member Posts: 226
    vivek4121 wrote: »
    Hi All,

    Just an update, with the below code added on "OnValidate Trigger" of No. Field we cannot create even the second purchase Order with same Item if the first Purchase order is not fully received and invoiced (and moved to achieve)
    That's because you haven't delivered clear specifications and the code doesen't care for document type or number.
    Should entering the same item be allowed in a different purchase document or not? Should it be possible
    to enter the same item a second time in the same document if the first item has already been delivered?
    there are lots of possible conditions to consider before asking for code.
Sign In or Register to comment.