Recalculate on Validate
Sales wants a pricing helper on items: enter a markup percentage, get a suggested sales price computed from the unit cost. The recalculation must happen when the field is validated — and only then. That's the whole difference between Item.Validate("Markup %", 25) and Item."Markup %" := 25, and one of the first things every AL developer has to internalize.
Requirements
Create a table extension that extends the
Itemtable.Add a field named
"Markup %"of typeDecimal.Add a field named
"Suggested Price"of typeDecimal.Give
"Markup %"anOnValidatetrigger that recalculates the suggested price asSuggested Price = Unit Cost × (1 + Markup % / 100)
rounded to the nearest 0.01. Translating that into AL — the field quoting, the
Roundcall, the trigger — is your job.
Because the logic lives in the OnValidate trigger, a plain assignment to "Markup %" must leave "Suggested Price" untouched — AL gives you that for free; don't recalculate anywhere else (not in OnModify, not in the other field).
What the tests check
The grading tests validate a generated markup against a generated unit cost and compare with the formula above, verify that a plain assignment followed by Modify(true) does not recalculate, and check the rounding from both sides: 33.33 * 1.10 = 36.663 → 36.66 and 33.33 * 1.20 = 39.996 → 40.00 (truncating is not rounding).
Learn More
OnValidate trigger to "Markup %" — it runs only when the field is changed through Validate()."Unit Cost" and write "Suggested Price" directly."Unit Cost" by (1 + markup / 100) and round with Round(Value, 0.01) — Round's second parameter is the precision (0.01), not a number of decimals.