0042

Recalculate on Validate

Basics
Easy
table-extension
onvalidate
field-triggers

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

  1. Create a table extension that extends the Item table.

  2. Add a field named "Markup %" of type Decimal.

  3. Add a field named "Suggested Price" of type Decimal.

  4. Give "Markup %" an OnValidate trigger that recalculates the suggested price as

    Suggested Price = Unit Cost × (1 + Markup % / 100)

    rounded to the nearest 0.01. Translating that into AL — the field quoting, the Round call, 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

Hint 1
Add an OnValidate trigger to "Markup %" — it runs only when the field is changed through Validate().
Hint 2
Inside a field trigger you can read "Unit Cost" and write "Suggested Price" directly.
Hint 3
Multiply "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.
ALBusiness Central 28.5
Press Compile to check your code compiles — Submit runs the tests.
The code editor is desktop-only
Open this problem on a computer to write and run code. Reading the description, tests and discussion works fine here.