D365 Business Central : Conditional Breakpoint on Debugging

Conditional Breakpoint

If you are a developer, you know how important it is to debug your code to find errors and understand how the code works. Debugging can be a difficult and time-consuming task, especially when you have to deal with complex or repetitive code. In this blog post, I will show you how to use a feature called conditional breakpoints in AL, which can help you debug your code faster and easier.

What are Breakpoints?

Before we talk about conditional breakpoints, let’s first talk about Breakpoints. Breakpoints are points in the code where you tell the debugger to pause the execution of the program so that you can check if everything is working as expected. You can use breakpoints to inspect the variables’ values or the code’s logic.

To set a breakpoint in AL, click on the left margin of the code editor, next to the line of code where you want to pause. A red dot will appear, indicating that a breakpoint has been set. You can also right-click and select Add Breakpoint from the menu.

To illustrate how breakpoints work, let’s create a simple example. Suppose we have a page extension for the Customer List page, where we add an action called Loop Action. This action will loop 50 times and calculate an amount based on a formula.

pageextension 60120 "Customer List_TNG" extends "Customer List"
{
    actions
    {
        addlast(processing)
        {
            action(LoopAction)
            {
                Caption = 'Loop Action';
                ToolTip = 'Loop Action';
                Image = Process;
                ApplicationArea = All;
                trigger OnAction()
                var
                    i: Integer;
                    MyAmount: Decimal;
                begin
                    MyAmount := 100;
                    for i := 1 to 50 do
                        MyAmount := MyAmount + (MyAmount * i);
                end;
            }
        }
    }
}

Let’s say we want to check the value of MyAmount after each iteration of the loop. To do that, we can set a breakpoint on the line where MyAmount is calculated.

Setting a breakpoint

When we run the action from the page, the debugger will stop at the first iteration of the loop. We can see the values of i and MyAmount in the Variables or Watch window. To continue the execution, we can press F5 or click on Continue in the toolbar. The debugger will stop at the next iteration of the loop. We can repeat this process until we reach the end of the loop, or until we find an error or a problem in our code.

Breakpoint Loop

However, this method can be very tedious and time-consuming, especially if we have to go through many iterations of the loop. For example, if we want to check the value of MyAmount when i is 15, we have to press F5 or click on Continue 14 times.

This is where conditional breakpoints come in handy.

Conditional Breakpoints

Conditional breakpoints are breakpoints that only pause the execution when a certain condition is met. The condition can be any expression that evaluates to true or false, using variables or fields that are in scope at that point in the code. You can use conditional breakpoints to skip unnecessary iterations of a loop, or to focus on specific cases or scenarios that interest you.

You can use any valid AL expression as a condition for your breakpoint. You can also use comparison operators, such as =, <>, <, >, <=, or >=, to compare variables or fields to other variables or fields, or to literal values. However, the variables or fields that you use in your condition must be of one of the following data types: BigInteger, Boolean, Code, Decimal, Integer, Option, or Text.

To set a conditional breakpoint in AL, you need to right-click on an existing breakpoint and select Edit Breakpoint from the menu. A dialog box will appear, where you can enter your condition in the text box.

You can also right-click on the left margin of the code editor, and select Add Conditional Breakpoint from the menu.

Editing a Breakpoint

For example, if we want to pause only when i is 15, we can enter i = 15 as our condition.

Setting Conditional Breakpoint

The breakpoint will change its appearance, indicating that it is now a conditional breakpoint. You can also hover over it to see its condition.

Conditional Breakpoint Mark

When we rerun the action, the debugger will skip all the iterations until i is 15, then pause. This way, we can save time and effort by only stopping at the iteration we care about.

Using Conditional Breakpoint

Conclusion

Conditional breakpoints are a powerful feature that can help you debug your code faster and easier. You can use them to pause the execution only when a specific condition is met and skip the rest of the iterations or executions that are not relevant to your problem. Conditional breakpoints can save you time and frustration.

I hope you found this blog post helpful and informative. Happy debugging!

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

Leave a Reply

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