How To Debug D365 Part 2

Share this:

In this previous article I explained how to debug D365 by attaching to an existing process. This is useful if you are debugging code on objects that do not exist in your solution. In this article, I will teach you how to debug D365 objects that are in your solution. As well as set the Model property on your project to allow you to add objects to your solution.

Open Visual Studio

First, start Visual Studio as ‘Run as administrator‘. Do this by clicking in the windows start button, and typing in ‘Visual Studio’. When the application shows up in the search results, right click on the application and select ‘Run as administrator‘.

Create Or Open A Dynamics 365 Project

Select File>New>Project. In the dialog that opens, select Dynamics 365 on the left hand side. Then select Finance Operations as the project type.

Note: If you do not see this option, you most likely do not have this project type installed. I recommend working in a Life Cycle Services Development environment, which will already have this installed and setup.

Enter in a name for your project, and check the ‘Create directory for solution’ checkbox. You do not need to check the ‘Add to Source Control’ checkbox, but you can if you have already setup your source control.

Next, right click on your project, and select Properties. In the dialog that opens, set the ‘Model’ of the objects you plan to create or debug. Typically, you will set this to the custom model that you have created. Any new objects you create in the solution will be stored in this model. Most developers plan to create new objects and so will create a solution associated with their model.

However, you can set Model property to an existing model that you do not own. This will let you add objects, that are part of that model, to the solution. You can tell what model an object exists in, by finding the object in the Application Explorer, and reading the text in between the square brackets.

As you will see later, having the object in the solution, allows Visual Studio to debug D365 without you needing to explicitly attach to a service.

Create Or Add An Object To The Solution

In Visual Studio, users can add or create D365 objects. In order to demonstrate how to debug D365, a form, or class with a ‘main’ method needs to be in the project.

For this example, let’s add the form ‘InventItemGroup’ to our solution. First, right click the project in the Solution Explorer, and select properties. Set the Model property to ‘Application Suite’. Then click Ok.

Next, enter ‘InventItemGroup’ in the Application Explorer search box and hit Enter. Locate the InventItemGroup node, right click and select ‘Add to project’.

In the Solution Explorer, right click the ‘InventItemGroup’ node, and select ‘Set as Startup Object’. This tells Visual Studio to open this object when the ‘Start’ button is pushed.

Set as Startup Object

Before we do start debugging the form, we need to add breakpoints.

Add Breakpoints

In order to add breakpoints, we need to open a code editor window. To view the code for the form, right click on the ‘InventItemGroup’ object and select ‘View Code’.

When the code editor window opens, select the first line of the ‘Init’ method. In my version, this is line 6. Click F9 to toggle adding a breakpoint. Alternatively, click on the grey bar just to the left of the line of code. This also will add a breakpoint. You can validate that the breakpoint was added, by seeing that a red circle has been added.

Debug D365 Form

Click the ‘Start’ button in Visual Studio to start debugging the form. You can also push the F5 key, as a shortcut.

Visual Studio will open a browser and direct D365 to open the form that you have specified as your startup object. If you had specified a class, Visual Studio will tell D365 to run the class.

When the system begins debugging, the red circles, which are your debug symbols may show as empty circles at first. After some time, the debug symbols should load, and become filled in red circles. If they do not, please read this other article on how to resolve this issue.

If everything works as expected, the system will stop on your first breakpoint, and your screen will look like the following.

Debugging D365 using Visual Studio

In the Visual Studio toolbar, notice the ‘Continue’ button. Pushing this, (or F5), will tell the system to run to the next breakpoint. There is also a ‘Stop’ button that can be pushed to stop debugging. There are several additional buttons in the toolbar that will assist you in stepping over, into, or out of X++ code.

Push the ‘Continue’ button now, and watch the form open, and interact with the user interface.

When you are ready to stop debugging, press the ‘Stop’ button in the Visual Studio toolbar.

Conclusion

Microsoft Visual Studio provides a very easy way for us to debug D365 objects added to Dynamics 365 Finance And Operations project. Users who are familiar with debugging their code in other languages can enjoy the same functionality in X++. And significantly, developers who used the old debugger in Dynamics AX 2012 and before, can take advantage of all the wonderful debug features that Visual Studio has to offer.

Peter Ramer
Peter Ramer is a part of the Managed Application Services team at RSM working on Microsoft Dynamics 365. He focuses on the Retail and Commerce industries. When he is not solving problems and finding ways to accelerate his clients' business, he enjoys time with his three kids and amazing wife.

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑