Ask Only When It Matters
The sales back office releases dozens of orders a day. An order released without the customer's purchase-order reference tends to come back as a disputed invoice, so releasing one deserves a deliberate Yes from a human. But most orders are fine — and a routine that asks "Are you sure?" every single time trains users to click Yes without reading, and crashes outright when it runs from a job queue where nobody can answer.
Requirements
Create a codeunit named "Order Release Manager" with one public procedure:
procedure ReleaseOrder(var SalesHeader: Record "Sales Header"): BooleanRules:
- If the order's
"External Document No."is empty, ask the user for confirmation with exactly this question:Order %1 has no external document number. Release it anyway?— where%1is the order's"No.". Note the exact wording, including the final question mark. - If the user answers Yes, release the order and return
true. - If the user answers No, leave the order exactly as it is and return
false. - If
"External Document No."is filled, release the order without showing any dialog and returntrue— the clean path must never prompt. - Releasing means setting the order's
Statusfield toReleasedand saving the record withModify(). Do not call the standard release codeunit — the graded orders have no lines, so it would refuse to release them.
Raise the dialog through the Confirm Management codeunit from the System Application — its GetResponseOrDefault(Question, Default) shows the dialog only when a UI exists and returns the default you pass otherwise. Pass false as the default: when the routine runs headless (job queue, web service), the risky release is then skipped instead of crashing the session. A raw Confirm call behaves the same under the grading tests but raises an error in headless sessions — LinterCop rule LC0021 exists precisely to catch it. Which of the two APIs you call is not graded; the prompting behavior is.
Pick object IDs in the 50100–50199 range, and reference other objects by name, never by ID.
What the tests check
Three tests grade the three paths. One answers Yes through a ConfirmHandler that also verifies the full question text with the real order number substituted, then expects the order Released and true returned. One answers No and expects the order still Open and false returned. The third seeds an order with an external document number and declares no ConfirmHandler at all — in the AL test framework any dialog without a handler fails the test, so a routine that always asks fails the clean path.
Learn More
- Confirm Management codeunit — the System Application wrapper this task is about:
GetResponseOrDefaultandGetResponse. - Dialog.Confirm method — the raw dialog underneath, and why confirm questions end with a question mark.
- System.GuiAllowed method — how code detects a session that cannot show any UI.
- Progress windows, Message, Error, and Confirm methods — best practices for user-facing dialogs, including putting the text in a label.
Confirm Management; its GetResponseOrDefault(Question, Default) shows the dialog only when a UI exists and returns your default otherwise — that is why LinterCop LC0021 flags raw Confirm calls."External Document No." = '' first; build the question with StrSubstNo and the order's "No.", exit with false when the answer is No, and only then set Status to Released and call Modify().