How to Debug Standard D365FO Code and Find Which X++ Method Is Running
Learn how to find and debug the standard X++ code running behind D365FO processes using breakpoints, Find References, the Call Stack, table methods, and Trace Parser.
One of the annoying parts of working with D365FO is figuring out where Microsoft is actually doing something.
You click a button, post a journal, confirm a sales order, and a bunch of code runs behind the scenes.
Then you get a task that sounds simple:
Add a validation when this gets posted.
Cool. Where?
There are a few different ways to find the code, but you definitely don't want to start opening random classes and putting breakpoints everywhere.
Here's how I normally track it down.
Start with the button#
If I know what button the user is clicking, I'll usually start there.
Open the form in Visual Studio and find the control.
From there, check what the button is actually calling. It could be:
- A menu item
- A form method
- A datasource method
- A command
- A class
If it's using a menu item, open the menu item and check the Object property.
That usually gives you a pretty good starting point.
Use Find References#
If I already know a table, field, class, or method that's involved, I'll use Find References.
For example, let's say I know a particular method on SalesTable is involved.
Instead of searching through the entire codebase, I can find references to that method and see who's calling it.
Same thing with fields.
If you're trying to figure out where a field is being populated, find references to the field and start looking through the results.
A lot of the time, that's enough to get you where you need to go.
Set a breakpoint#
Once I find something that looks like the right place, I'll put a breakpoint on it and run the process.
If it gets hit, great.
Now I know I'm in the right area.
From here, you can Step Into and Step Over the code, but I normally check the Call Stack before I start going too far.
The Call Stack is your friend#
If you're not using the Call Stack when debugging D365FO, you're making things harder than they need to be.
Once your breakpoint gets hit, go to:
Debug > Windows > Call Stack
You'll see something along these lines:
ClassA::run()
ClassB::process()
ClassC::create()
SomeTable::insert()Now you can see exactly how you got to the method you're currently sitting in.
This is especially useful with standard Microsoft code because there might be 20 classes involved in what looked like a simple button click.
Instead of trying to understand all 20 classes, look through the Call Stack and figure out which ones actually matter.
This is also usually how I figure out what class or method I need to extend.
Sometimes it's easier to start from the table#
This is probably the approach I use the most.
Sometimes I have absolutely no idea where the process starts, but I know what happens.
For example:
When I post this journal, a record gets created in this table.
Perfect.
I don't really care where the process starts yet.
I'll go to the table and put a breakpoint somewhere I know the process has to hit.
Depending on what I'm looking for, that could be:
insert()
update()
delete()
validateWrite()
modifiedField()Run the process again.
Once the breakpoint gets hit, check the Call Stack.
Now I can work backward and see exactly what class caused the table to be updated.
For some of the larger D365FO processes, this is much easier than starting at the button and stepping through everything.
What if I'm trying to find an error message?#
This happens all the time.
You get something like:
Something cannot be posted because...
You search the entire codebase for that exact message.
Nothing.
There's a good chance it's coming from a label.
The actual code might look like this:
error("@SYS12345");So searching for the text you see on the screen isn't always going to find it.
If you can figure out which label is being used, search for the label ID instead.
That will usually get you much closer to the code generating the message.
You can also break when the Infolog message gets added#
If I can't find the error through search, I'll sometimes let the debugger find it for me.
Put a breakpoint where the Infolog message is being added and reproduce the issue.
Once the breakpoint gets hit, check the Call Stack.
Then just work your way backward until you find the code that actually generated the message.
I've used this plenty of times when I knew exactly what error I was looking for but had no idea where Microsoft was throwing it.
My breakpoint isn't getting hit#
Before you spend an hour questioning everything, make sure you're actually debugging the right process.
Not everything in D365FO runs through the same process.
You could be dealing with:
- Normal interactive processing
- Server-side processing
- Batch
- A service
- Some type of asynchronous processing
Batch is an easy one to get caught by.
You can have a breakpoint in the exact method that's running and it will never get hit if you're not attached to the process actually executing the code.
So if you're pretty sure you found the right method but your breakpoint isn't doing anything, check that before you start looking somewhere else.
What about Trace Parser?#
Sometimes breakpoints aren't the best option.
Think about something like posting, MRP, warehouse processing, or another big standard framework.
There can be a ridiculous amount of code running.
You don't want to Step Into every method and hope you eventually find what you're looking for.
That's where Trace Parser can help.
You can capture what happened and then look through the methods that were actually executed.
I wouldn't use it for every little debugging task, but when you're dealing with a huge process and have no idea where to start, it can save you a lot of time.
How I normally approach it#
If I know where the process starts:
Button / Menu Item
↓
Find the entry point
↓
Set a breakpoint
↓
Run the process
↓
Check the Call Stack
↓
Follow the code that actually mattersIf I don't know where it starts, but I know what table is being changed:
Table
↓
Breakpoint on insert/update
↓
Run the process
↓
Check the Call Stack
↓
Find the class doing the workI probably use the second approach more often.
Don't try to understand the entire process#
This is where I think people make debugging D365FO harder than it needs to be.
You don't need to understand the entire posting framework just because you're changing one validation.
You don't need to understand every class involved in creating a sales order just because you need to change how one field gets populated.
Figure out what you actually care about and find a point in the code you know has to execute.
Set a breakpoint there.
Run the process.
Check the Call Stack.
Then work your way backward or forward from there.
Once you get used to doing that, finding where Microsoft is doing something becomes a lot easier.
Missing something?
Suggest a topic you'd like to see covered next. The most requested topics help shape future XppForge documentation.
Sign in to suggest a topic