0058

Enforce a Customer Order Cap at Release

event-subscriber
table-extension
sales
release

Enforce a Customer Order Cap at Release

Credit control has negotiated a hard ceiling with some customers: no single sales order above an agreed amount. Sales users keep releasing orders past that ceiling, and by the time anyone notices, the warehouse has already picked the goods. The release step is handled by base code — codeunit 414 "Release Sales Document" — which you cannot modify. Your job is to make an over-cap order impossible to release, from the outside.

Requirements

  1. Create a table extension for the Customer table with a field named "Max Order Amount (LCY)" of type Decimal.
  2. Create a new codeunit — its name is up to you (not graded) — that blocks the release of a sales order whose amount exceeds the sell-to customer's "Max Order Amount (LCY)". Do not try to change base objects; extend them from the outside.

Rules the tests enforce:

  • The cap applies only when a sales document of type Order is released. Other document types (quotes, invoices, credit memos, ...) must release regardless of the cap.
  • The cap is read from the order's sell-to customer.
  • The order's amount is the sales header's Amount — the sum of its line amounts excluding VAT. The graded documents carry no currency code, so that amount is already in LCY; currency conversion is out of scope and not graded.
  • Over the cap means strictly greater: an order whose amount equals the cap exactly must still release.
  • A "Max Order Amount (LCY)" of 0 means the customer has no cap — any amount releases.
  • Blocking must happen by raising an error, and the error message must contain the exact text Max Order Amount (LCY) (the rest of the wording is up to you).
  • A blocked order must be left untouched: its Status stays Open.

Pick all object and field IDs in the 50100–50199 range, and reference other objects by name, never by ID. No page extensions are needed — grading reads and writes the field in code. Captions and tooltips are good practice but not graded.

What the tests check

The tests create customers with generated caps and sales documents whose line amounts they control exactly, then release them through the standard release routine. They assert that releasing an over-cap order fails with an error containing Max Order Amount (LCY) and that the order's Status is still Open afterwards; that an under-cap order and an order exactly at the cap both reach Status Released; that a customer with a cap of 0 can release an order of any amount; that an over-cap sales invoice and an over-cap quote both still release, because the cap governs orders only; and that an order is still blocked when its bill-to customer differs from the sell-to customer and has no cap — the cap must come from the sell-to customer.

Learn More

Hint 1
You cannot edit codeunit 414 "Release Sales Document", but you don't need to: it publishes integration events, and a subscriber in your own codeunit runs as part of the release routine — an error raised there stops the release before the status ever changes. In VS Code, Shift+Alt+E lists every event you can subscribe to.
Hint 2
Look for the event the release codeunit raises before it does any work — it hands you the Sales Header being released. In your subscriber, exit early unless the document type is Order and the sell-to customer has a nonzero cap. The header's Amount is a FlowField: it reads as 0 until you calculate it.
Hint 3
The subscriber is [EventSubscriber(ObjectType::Codeunit, Codeunit::"Release Sales Document", 'OnBeforeReleaseSalesDoc', '', false, false)] on a procedure taking var SalesHeader: Record "Sales Header" (a subscriber may declare only the parameters it needs). Get the sell-to customer, call SalesHeader.CalcFields(Amount), and raise an error mentioning "Max Order Amount (LCY)" when the amount is strictly greater than the cap.
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.