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;
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;
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;
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
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.
Answers
Slawek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
I sense that check must cover every line so:
It should be more like this:
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
Better way is to handle it right way on the No column as soon as User enters the same Item No.
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
Checking the same for every line the code provided by you will work.
Thanks
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');
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)
Seems not feasible for all scenarios.
Thanks in advance.
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
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
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.