MSBA-340 · Lesson 10 of 12 · 19 min

Debugging

▶ Video coming soon A video version of this lesson is on the way.

Prudence's first real bug arrived on a Tuesday. She registered a warranty for a pump installed on the last day of October with a 24-month term, and the warranty form showed it ending on the thirtieth of October two years later, a day earlier than Teodora expected. The tenth clinic used that bug as its worked example from start to finish. Desmond's view was that debugging is a method rather than a talent, and that the unified developer experience changes the mechanics but not the method.

The debugging method

Desmond wrote five steps on the whiteboard and asked Prudence to follow them in order, every time, before touching any code.

  • Reproduce the problem reliably, with the smallest possible example.
  • Form a hypothesis about where the wrong value comes from.
  • Observe: set breakpoints or traces where the hypothesis says the value is created.
  • Fix the cause, not the symptom, and add a test that would have caught it.
  • Deploy, check the original case and the test, and check nothing nearby broke.

For the end-date bug, reproducing it was easy: an install date of October 31, 2026 and 24 months. The hypothesis was that the calculator from lesson 4 was wrong, because it was the only code that computed end dates. Observing it meant attaching the debugger to the code running in her environment.

Building and deploying before debugging

Because X++ runs only in the cloud tier of the unified developer environment, the code being debugged must be built and deployed there first. A breakpoint on code that has not been deployed will never be hit, which is the most common source of confusion for developers new to the unified experience. Visual Studio offers several ways to deploy, with different effects on the database.

Deploy options in the unified developer experience, from the Microsoft Learn tutorial on writing, deploying and debugging X++.
CommandWhat it deploysSynchronizes the database?
Dynamics 365 > Deploy > Deploy models to online environmentThe selected models in fullOptional, chosen in the dialog
Dynamics 365 > Build models with Deploy to connected online environmentFull build, then full deployment of the modelsAccording to the Synchronize database option
Incremental build with the project property Deploy changes to online environmentOnly changes since the last successful deploymentAccording to the project's Synchronize database property
Project > Deploy model for projectOnly changes since the last successful deploymentNo
Dynamics 365 > Synchronize databaseNothing new; synchronizes all modelsYes
A left-to-right flow. Edit code in Visual Studio, then Build, then Deploy to the unified developer environment, with a branch labelled synchronize database when tables changed. From the deployed environment, an arrow to Launch debugger and another to Run in browser, both leading to Breakpoint hit. A return arrow from Breakpoint hit back to Edit code is labelled fix and redeploy.
The edit, build, deploy and debug cycle.

The rule Desmond gave was simple: if a table, field, index or enum changed, synchronize; if only code changed, an incremental deployment without synchronization is enough and is faster. Progress appears in the Output window under the FinOps Cloud Runtime pane, with a link to the deployment log when it finishes. When a deployment fails, the log is the first place to look, and the most common causes are a compile error the developer ignored, a missing package reference, or a synchronization problem such as a new unique index meeting duplicate data.

Setting up the debugger

Visual Studio needs symbols for the code it debugs. Under Extensions, the Dynamics 365 Options include a Debugging page where the developer chooses to load symbols only for the items in the solution, or also for selected packages. Microsoft's tutorial recommends loading symbols for the solution plus the Application Platform and Application Foundation packages; a developer stepping into Application Suite code adds that package too. Loading symbols for everything makes debugging slow, so it is better to add packages only when needed.

There are two ways to start debugging. Pressing F5 on a project whose startup object is a runnable class or a form starts the debugger and opens the browser at a URL that runs the object, so the debugger is already attached when the code starts. Alternatively, Dynamics 365, Launch debugger attaches the debugger to the application process already running in the environment, which is how a developer debugs code reached by clicking through the browser normally. When finished, Detach leaves the application running, whereas Stop can end the session.

URLs that open a runnable class, a form by its menu item, and the table browser, in the CRCA company. The environment address is a placeholder.
https://<environment-url>/?cmp=CRCA&mi=SysClassRunner&cls=CRPumpWarrantyCalculatorTest
https://<environment-url>/?cmp=CRCA&mi=CRPumpWarrantyTable
https://<environment-url>/?cmp=CRCA&mi=SysTableBrowser&tableName=CRPumpWarrantyTable

The URL parameters are worth learning. cmp sets the company, mi names a menu item, and the class runner menu item with a cls parameter runs a runnable class. Changing the mi parameter is a quick way to open any form directly while debugging, as long as the user has access to it.

Breakpoints and stepping

Breakpoints are set in the code editor by clicking in the margin or pressing F9. When execution reaches one, Visual Studio stops and shows the current line, the call stack, local variables, and the values of expressions in the Watch and Autos windows. F10 steps over a line, F11 steps into a method call, Shift+F11 steps out, and F5 continues to the next breakpoint. Conditional breakpoints stop only when an expression is true, such as when warranty.SerialNumber equals the problem serial number, which is invaluable inside loops over thousands of records.

Prudence set a breakpoint in the calculator's endDate method, ran the warranty form, opened the problem warranty and watched the values. installDate was October 31, 2026. dateMthFwd(installDate, 24) returned October 31, 2028, because adding months keeps the day of the month where it exists. Then the method subtracted one day, giving October 30, 2028. The code was doing exactly what it said. The hypothesis that the calculator was wrong was half right: the code was correct arithmetic, but the business rule was wrong.

She took the finding to Teodora, who explained that Cobalt Ridge's warranty terms read: the warranty runs for the stated number of months from the installation date, inclusive of the anniversary date. A pump installed on October 31 is covered through October 31 two years later. The minus one in the code had been Prudence's own assumption. The fix was to remove it, and to add a unit test with this exact case, which lesson 11 shows.

The Infolog and messages

Not every investigation needs the debugger. The Infolog shows info, warning and error messages, and a developer can add temporary info calls to print values while testing in their own environment. The Infolog also groups messages from one operation, and error messages often include the call stack of where they were raised when viewed by a developer. Desmond's rule was that temporary info calls never reach a pull request; lesson 11's review checklist catches them.

Exceptions shown in the browser deserve a careful read. A message like Cannot edit a record in Pump warranties, the record has never been selected, means code called update on a buffer that was not selected forupdate, as lesson 6 explained. A message about an update conflict means optimistic concurrency detected a change by someone else. Reading the message closely usually points straight at the lesson that explains it.

Tracing and Trace Parser

Some problems are not about wrong values but about time. When a form or a batch job is slow, the developer captures a trace of the operation and analyses it with Trace Parser, which is installed from the TraceParser.msi file downloaded with the development assets in lesson 1. A trace records X++ method calls with their durations and the SQL statements the kernel sent to the database, with counts and timings. Trace Parser shows the call tree, the slowest methods, the most frequent SQL statements and the statements that took longest, so a developer can see at a glance whether the time went into code, into many small queries, or into one expensive one.

Traces are captured from the browser, using the trace capture option in the settings menu, around the steps that are slow; the trace file is then opened in Trace Parser. The MB-500 outline asks candidates to capture traces and analyse them with Trace Parser, and MSBA-343 shows a full performance investigation. In the clinic, Prudence captured a trace of opening the warranty form and found that a display method she had written made one query per row of the grid. That was exactly the warning from lesson 7, and the trace made it impossible to argue with.

Finding where a value comes from

Many bugs are questions of origin: where does this default come from, who changed this field, which class posts this voucher? The debugger answers them for one run, but the cross-reference database from lesson 1 often answers them faster. Find references on a field lists every method that reads or writes it, across standard and custom code. For a default value, the usual suspects are the table's initValue, a data source's initValue on the form, an EDT's default, and code in the class that creates the record. Desmond taught Prudence to check them in that order and to set a breakpoint on each candidate rather than guessing.

Standard code can be read and stepped into once symbols for its package are loaded, which is often the quickest way to understand why the application behaves as it does. The code cannot be changed, and it should not be: when the standard behaviour must differ for Cobalt Ridge, the change belongs in an extension, as MSBA-341 explains. Stepping through standard code is research, not a step toward editing it.

Debugging batch and asynchronous work

Code that runs in batch runs in a separate process on the batch service, not in the user's session, so attaching the debugger to the interactive session does not stop at breakpoints in it. During development, the usual technique is to run the same logic interactively first, for example by running the job's dialog without the batch option, so that breakpoints are hit in the user session. When a problem only appears in batch, the batch job's history and the Infolog messages saved with each batch task are the first evidence, and the debugger can be attached to the batch process where the environment allows it. The SysOperation framework in MSBA-341 makes this easier by separating the service class, which does the work, from the controller that decides how it runs, so the service can be called and debugged directly.

Asynchronous operations started from forms, such as some posting operations that run in the background after the user clicks, behave similarly. The rule of thumb is the same: find the class that does the work, call it in a way that runs in the session, and debug it there before investigating the asynchronous path.

Monitoring running environments

In sandbox and production environments, developers do not attach debuggers. Microsoft's guidance is to use Application Insights for monitoring and diagnosing the running application, including telemetry about errors and performance, and the environment's monitoring tools for batch jobs, database activity and other health signals. Lifecycle Services has historically provided environment monitoring for finance and operations projects, and newer telemetry is exposed through Application Insights; MSBA-343 explains how Cobalt Ridge set up monitoring before go-live. For developers, the lesson is that code should leave useful evidence when it fails: clear error messages from labels, and enough context in them, such as the serial number, to identify the record involved.

Common problems in the unified developer experience

Desmond kept a list of the problems that new developers hit in their first month, and he gave Prudence the list at the end of the clinic.

Common problems in the first weeks of unified development.
SymptomLikely causeFix
Breakpoint is never hitLatest code not deployed, or symbols not loaded for that packageDeploy, then check the Debugging options
New field missing at run timeDeployed without database synchronizationRun Synchronize database or redeploy with sync
Form shows old layoutBrowser cache or deployment still runningWait for the Output pane to finish, then refresh
Build fails on an element from another packageMissing package referenceAdd the reference in Model management
Synchronization fails after adding an indexDuplicate data breaks a new unique indexClean the data, then synchronize
Sign-in to the environment fails from Visual StudioAccount lacks System Administrator, or multifactor options in the login dialogCheck the role and the connection options described in lesson 1

Worked problem

Debugging a validation that never fires

Teodora reports that she was able to save a warranty with an installation date next year, even though validateWrite should stop it. Prudence can reproduce it on the form. What should she do?

Show solution
  1. Reproduce: open the warranty form in CRCA, enter an install date in 2027, save. The record saves.
  2. Hypothesis: either validateWrite is not being called, or its condition is wrong.
  3. Observe: set a breakpoint at the start of validateWrite on the table, launch the debugger and save again. If the breakpoint is not hit, check that the latest code is deployed; if it is hit, step through the condition.
  4. In this case the breakpoint is hit, and the watch window shows the comparison uses the session date, which Teodora had changed in her user options for testing. The code compared against a future session date.
  5. Fix: compare against the system date from DateTimeUtil, as lesson 3 advised, add a unit test, deploy and retest.

Answer: The validation fired but compared against the session date; using the system date fixed it.

Common mistakes

  • MistakeSetting breakpoints before deploying the latest code.

    InsteadBuild and deploy first; X++ runs only in the cloud environment.

  • MistakeUsing Deploy model for project after changing a table.

    InsteadSynchronize the database when tables, fields, indexes or enums change.

  • MistakeLoading symbols for every package.

    InsteadLoad symbols for the solution and the packages you step into, to keep debugging fast.

  • MistakeFixing the symptom when the business rule is unclear.

    InsteadConfirm the rule with the business owner, then fix the cause and add a test.

Summary and key terms

  • Debugging follows a method: reproduce, hypothesize, observe, fix the cause with a test, and verify.
  • Code must be built and deployed to the unified developer environment before it can be debugged; synchronize the database when the data model changes.
  • F5 on a startup object or Dynamics 365 > Launch debugger attaches Visual Studio; Detach leaves the application running.
  • Breakpoints, conditional breakpoints, stepping and watch windows show values; the Infolog shows messages and errors.
  • Trace Parser analyses captured traces of X++ calls and SQL; Application Insights monitors running environments.
Breakpoint
A marker in code where the debugger stops execution.
Launch debugger
The Dynamics 365 command that attaches Visual Studio to the running application.
Database synchronization
Updating the physical database to match deployed table metadata.
Trace Parser
A tool that analyses captured traces of X++ method calls and SQL statements.
Symbols
Debug information that lets Visual Studio map running code to source lines.
Application Insights
An Azure monitoring service used for telemetry from running environments.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Which deploy option never synchronizes the database?

    Show answer

    Deploy model for project from the project context menu.

  2. What do the cmp and mi URL parameters do?

    Show answer

    cmp sets the legal entity and mi names the menu item to open.

  3. Why was the end-date bug not a coding error?

    Show answer

    The code did what it said; the business rule it implemented was wrong, so the rule had to be confirmed and the code changed to match.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz