Hi Guys
I have seen both codes where some people use for Insert and Modify: true. Apparently from what I understand true invokes triggers but what I don't understand is when to use and when not to. For example, creating new order standard code is below, what's the benefit of true here on Insert/Modify?:
SalesHeader.INIT;
SalesHeader."Document Type" := SalesHeader."Document Type"::Order;
SalesHeader."No." := '';
SalesHeader.INSERT(TRUE);
SalesHeader.VALIDATE("Sell-to Customer No.",CustNo);
SalesHeader.VALIDATE("Location Code",LocCode);
SalesHeader.MODIFY;
SalesLine.INIT;
SalesLine."Document Type" := SalesHeader."Document Type";
SalesLine."Document No." := SalesHeader."No.";
NextLineNo := NextLineNo + 10000;
SalesLine."Line No." := NextLineNo;
SalesLine.INSERT(TRUE); //Run Trigger Code
SalesLine.VALIDATE(Type,SalesLine.Type::Item);
SalesLine.VALIDATE("No.",ItemNo);
SalesLine.VALIDATE("Location Code",LocCode);
SalesLine.VALIDATE("Unit Price",UnitPrice);
SalesLine.VALIDATE(Quantity,Qty);
SalesLine.MODIFY(True);
0
Answers
Fully agree on this.
Always go for "TRUE" by standard. It's only if you explicitly don't want to run this code, then you run with FALSE (or leave out).
OnValidate triggers are used for getting related data into other fields. The decision to validate or not is when you want to take care of those in your code yourself. For instance, you can validate a City field and that pulls in post code and such. If you are setting those fields yourself, there is no need for calling the validation trigger.
Same as Insert(true) or Modify(true). Those triggers do things and the only reason why you would NOT run them is when you are doing those things in your own code.
You'll get different opinions here though, I've had very heated discussions about this topic. Some ppl will vehemently say to NEVER run triggers, where I say you should absolutely run them as much as possible. For instance, validating the Quantity on the Sales Line will calculate amounts and discounts and such. If you don't validate, you will have to program that calculation yourself.
My approach is to first do a task manually, and then do it in code. Creating a new Sales Order for instance. You open the page with a blank record, and as soon as you leave the No field, it inserts the order with a new number from the number series. That's the first 4 lines in the code you posted. Validate the Customer and the Location pulls in all sorts of important information. That's just three more lines of code. In 6-7 lines of code, you have a complete sales order, all you need to do is add some lines. I don't see any reason why anyone would ever program all of that.
Here's a good tip: turn on the debugger and do things in BC. Step through the code to get a feel for what it does.
RIS Plus, LLC