MSBA-352 · Lesson 11 of 12 · 18 min
Debugging AL
▶ Video coming soon A video version of this lesson is on the way.
In the third week of November, Callum ran his end-to-end test script in UPDATE-TEST and found something odd. Bayside Café had ordered two espresso grinders. The warehouse shipped them on Monday and the order was invoiced on Wednesday. The registrations list showed four registrations for Bayside, two of them with Wednesday's date and Monday's shipment number. Nothing had failed and no error had appeared. "This is the worst kind of bug," Noor said. "It is quiet." Finding it took Joss through every debugging tool Business Central offers, and this lesson follows the hunt.
The debugging toolbox
| Tool | Where it works | Best for |
|---|---|---|
| Launch debugging (F5) | Containers, on-premises, online sandboxes | Stepping through code you just changed |
| Attach and debug next | Containers, on-premises, online sandboxes | Catching a session started by someone else, a web service call or a background task |
| Snapshot debugging | All environments, including production | Recording a problem as it happens and debugging it afterwards |
| Profiler | Wherever snapshot or launch debugging works | Finding which code is slow |
| Event Recorder, Page Inspection | The client, any environment | Seeing which events fire and which objects are behind a page |
| Code analyzers | Visual Studio Code | Finding problems before the code runs |
Breakpoints and stepping
The ordinary debugger works like any modern one. Joss set a breakpoint with F9 on the first line of the warranty subscriber, pressed F5 with the container configuration selected, and posted a shipment in the browser that opened. Execution stopped at the breakpoint and Visual Studio Code showed the debugger.
| Key | Action |
|---|---|
| F9 | Toggle a breakpoint on the current line |
| F5 | Start debugging, or continue to the next breakpoint |
| F10 | Step over: run the current line, including any procedure it calls |
| F11 | Step into: go inside the procedure the current line calls |
| Shift+F11 | Step out: run to the end of the current procedure |
| Shift+F5 | Stop debugging |
The Variables pane shows local and global variables, including record variables with every field of the current row and the filters on the variable. The Watch pane keeps chosen expressions in view. The Call Stack shows how execution got here: Joss could see the click on Post, the Sales-Post codeunit, the event raise and finally the subscriber. The Debug Console evaluates expressions while stopped. And when the debugger stops on a database operation, it also shows recent SQL statements and how long they took, which helps spot slow code.
Two settings in launch.json make the debugger stop without breakpoints. breakOnError stops whenever an error is raised: All (the default), None, or ExcludeTry, which skips errors caught by try functions. breakOnRecordWrite stops every time a record is written: None (the default), All, or ExcludeTemporary to ignore temporary tables. Joss turned on breakOnRecordWrite with ExcludeTemporary for this hunt, which made the debugger stop on every insert into any table and showed exactly when each registration was written.
Debugging Microsoft's code works too, as far as symbols allow. Stepping into Sales-Post showed the standard code, because Microsoft allows debugging its apps. Stepping into another partner's app works only if that app's resource exposure policy allows debugging (lesson 2).
Reproducing the bug
In the container, Joss shipped an order for two grinders, then invoiced it separately. The breakpoint in the subscriber hit twice: once for the shipment posting and once for the invoice posting. That was expected. On the first hit, SalesShptHdrNo held the new shipment number. On the second, it was empty and the subscriber exited at once. No duplicate. Joss could not reproduce Callum's bug.
Noor asked what was different in UPDATE-TEST. Joss checked the registrations there: the duplicates all came from orders invoiced through Get Shipment Lines on a separate sales invoice, the Combine Shipments process from the inventory course, not from posting the invoice on the order. That was a different code path. And Joss's published app in UPDATE-TEST was not the build in the container: it was the version from the previous Friday.
Attach and debug next
To see what happened in UPDATE-TEST itself, Joss used an attach configuration. Instead of publishing and opening a new session, attach waits for the next session of a given type and debugs it. The type is set with breakOnNext: WebClient for a user in the browser, WebServiceClient for API and web service calls, Background for job queue and other background sessions, and Agent for sessions run by agents. Adding userId restricts it to one user's next session; debugging another user's session needs the D365 ATTACH DEBUG permission set.
{
"name": "Attach to UPDATE-TEST (Callum)",
"type": "al",
"request": "attach",
"environmentType": "Sandbox",
"environmentName": "UPDATE-TEST",
"breakOnNext": "WebClient",
"userId": "<Callum's user security ID>",
"breakOnError": "All",
"breakOnRecordWrite": "None"
}Joss published the current build to UPDATE-TEST with Ctrl+F5, set a breakpoint in the subscriber, started the attach configuration, and asked Callum to sign out and back in, so that his next session was new. Refreshing a browser tab can reuse an existing session, and attach waits for a new one. Callum created a sales invoice, used Get Shipment Lines to pull in Bayside's Monday shipment, and posted. The breakpoint hit, and SalesShptHdrNo was empty, exactly as it should be. Again no duplicate.
"So the current code is fine," Noor said. "The duplicates came from the old build. What did Friday's version do?"
Snapshot debugging
The Friday build was gone from Joss's machine, but it was in the Git history, and the same problem might be lurking in production one day where attach is not allowed. Noor used the moment to teach snapshot debugging, the only debugging that works in an online production environment.
A snapshot debugging session records code execution on the server for a chosen user or session type, and the developer debugs the recording afterwards in Visual Studio Code. Nothing stops on the server; users notice nothing. Joss checked out Friday's commit, published it to UPDATE-TEST, and ran the session.
{
"name": "Snapshot UPDATE-TEST",
"type": "al",
"request": "snapshotInitialize",
"environmentType": "Sandbox",
"environmentName": "UPDATE-TEST",
"breakOnNext": "WebClient",
"userId": "<Callum's user security ID>",
"executionContext": "DebugAndProfile",
"snapshotVerbosity": "SnapPoint"
}| Step | How |
|---|---|
| Set snappoints | Like breakpoints, but they record variable values instead of stopping |
| Initialize | F7, or AL: Initialize Snapshot Debugging; the server waits for a matching session |
| Reproduce | The user performs the action; the server records |
| Check status | Shift+F7 lists sessions: Initialized, Started, Finished, Downloaded |
| Finish and download | Alt+F7 ends recording and downloads the snapshot file to the workspace |
| Replay | Open the snapshot in the debugger and step through it, stopping at snappoints |
Some limits matter. An initialized session closes if no matching session starts within 30 minutes, and a started session must be finished within 10 minutes. Variable values are captured only at snappoints and where AL errors occur; the default verbosity records at snappoints, and a Full setting records every line but is expensive and meant for short, targeted runs. The code with the snappoints must already be deployed, and the symbols on Joss's machine must match the server's exactly, or lines will not line up. The user who creates and downloads snapshots needs the D365 SNAPSHOT DEBUG permission set.
Noor added a privacy warning. A snapshot records real variable values, so in production it can contain customer and personal data. Quarrystone treats snapshot files like any customer data: stored only as long as needed and deleted when the problem is solved.
The bug
Replaying the snapshot of Friday's build, Joss stepped into the subscriber and saw it. Friday's version had not used the SalesShptHdrNo parameter at all. It had read SalesHeader."Last Shipping No.", a field on the sales header that holds the number of the most recent shipment posted for that order. When the order was shipped on Monday, Last Shipping No. was Monday's shipment, and registrations were created. When the invoice was posted on Wednesday, no new shipment was created, but Last Shipping No. still held Monday's shipment number, so the subscriber registered the same two serial numbers again with Wednesday's date. The duplicate check in AlreadyRegistered had a bug of its own in that build: it filtered on the shipment date rather than on the active status, so it found nothing.
Joss had already fixed both, without knowing it, when rewriting the subscriber after a review comment from Noor ("use the parameter the event gives you, not a field that means something else"). The snapshot proved the old build was the culprit and the new one was not. Callum voided the duplicate registrations in UPDATE-TEST, and Joss added a test case to the script: ship, then invoice later, then check that there are exactly two registrations.
Finding slow code with the profiler
While the snapshot was open, Noor showed the profile half of it. With executionContext set to DebugAndProfile, the snapshot also records how long each method took, and Visual Studio Code shows it as a tree of calls with time spent in each. Joss saw that the warranty subscriber took a few milliseconds out of a posting that took most of a second, almost all of it in standard code. That was the answer to a question Bettina had asked earlier: does the warranty app slow down shipping? No, and now there was evidence. The client also has a Performance Profiler page that records a user's session without Visual Studio Code, useful for asking a power user to capture a slow process themselves.
Debugging without a debugger
Not every problem needs the debugger. Joss built a short list of first checks for Callum and the support team.
- Read the error carefully and use Copy details, which includes the call stack of AL code at the point of the error, the user, the company and the environment. Many problems are diagnosed from that text alone.
- Page Inspection (Ctrl+Alt+F1) shows the page, table, fields and extensions behind a screen, and whether the warranty app has added anything to it.
- Event Recorder shows which events fired during an action, which answers "did our subscriber even run?"
- Extension Management shows which version of the warranty app is installed, which would have exposed the stale Friday build immediately.
- Telemetry, sent to Azure Application Insights, records errors, long-running operations and custom signals from AL in every environment, including production. Setting it up and adding custom telemetry is covered in the next course.
Code analysis catches bugs before they run
Joss also went back to the analyzers from lesson 1. CodeCop had flagged the Friday subscriber with a warning that Joss had ignored: a local variable, ShipmentNo, declared and never used. Joss had meant to fill it from the event's shipment parameter and then read the header field instead. That warning was the bug in plain sight. Quarrystone's ruleset now raises the unused-variable rule to an error.
AI-assisted debugging
The AL agent tools from lesson 1 include debugging tools: GitHub Copilot in agent mode can start a debug session, set breakpoints, and manage snapshot debugging sessions, and Microsoft documents a snapshot debugging workflow for MCP-compatible agents from the 2026 release wave 2. Joss tried asking Copilot to explain the snapshot. It summarized the call path well and pointed at the Last Shipping No. line, but it also suggested "fixing" the duplicate check by deleting the unique key, which would have hidden the problem. Noor's rule held: AI can read the evidence quickly; a person decides the fix.
Worked problem
Choosing a debugging mode
Three problems arrive after go-live: a calculation error Joss can reproduce in the container, a failure that happens only when the web shop calls the warranty API in UPDATE-TEST, and a wrong end date that a café reported in production. Which mode fits each?
Show solution
- Reproducible in the container: launch debugging with F5 and breakpoints.
- API calls in UPDATE-TEST: attach with breakOnNext set to WebServiceClient, and ask the web shop to make one call.
- Production: snapshot debugging with snappoints in the end date calculation, with Callum reproducing the case under his user, and the snapshot deleted afterwards.
- In every case, check first which app version is installed in that environment.
Answer: Launch where you can reproduce, attach for sessions started elsewhere in sandboxes, snapshot in production.
Common mistakes
MistakeDebugging without checking which app version the environment runs.
InsteadCheck Extension Management first; a stale build is a common cause.
MistakeTrying to attach a live debugger to production.
InsteadUse snapshot debugging, the supported option for production.
MistakeRefreshing the browser and waiting for the attach session to start.
InsteadStart a new session, for example by signing out and in.
MistakeKeeping snapshot files after the problem is solved.
InsteadTreat snapshots as customer data and delete them when done.
MistakeIgnoring analyzer warnings about unused variables.
InsteadRead every warning; some are bugs in plain sight.
Summary and key terms
- Launch debugging with breakpoints, stepping, variables, call stack and SQL information works in containers and sandboxes.
- breakOnError and breakOnRecordWrite stop the debugger without breakpoints.
- Attach debugs the next session of a chosen type, including web service, background and agent sessions.
- Snapshot debugging records execution for later replay and is the only debugging in production.
- Profiling shows where time is spent; telemetry covers production at scale.
- Error details, Page Inspection, the Event Recorder and analyzer warnings solve many problems before a debugger is needed.
- Breakpoint
- A marker where the debugger stops execution.
- Attach
- A debugging mode that joins the next session of a given type instead of publishing.
- breakOnNext
- The launch.json setting naming the session type to attach to or record.
- Snapshot debugging
- Recording server-side execution for later debugging, supported in production.
- Snappoint
- A point where a snapshot records variable values without stopping.
- Profiler
- A tool that records time spent in each method.
- Call stack
- The chain of calls that led to the current line.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Why did the duplicates not appear in Joss's container?
Show answer
The container ran the current build, which used the SalesShptHdrNo parameter; UPDATE-TEST ran Friday's build, which read Last Shipping No.
Which debugging modes can be used against Corbel & Finch's PROD environment?
Show answer
Only snapshot debugging; launch publishing and live attach are not supported in production.
What permission set does a user need to create and download snapshots?
Show answer
D365 SNAPSHOT DEBUG.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.