Quantcast
Channel: Dynamics AX
Viewing all articles
Browse latest Browse all 550

X++ Debugging Tips and Tricks #3–Conditional breakpoints

$
0
0

The Dynamics AX Debugger doesn’t support conditional breakpoints. Once in a while this limitation can be quite annoying – but there is a simple workaround. The third trick in the series of X++ Debugging Tips and Tricks shows you how.

This trick requires code instrumentation – I strongly discourage using this trick on a production system. Only use this trick in a test or development sandbox!        

By using the breakpoint keyword, you can write X++ logic that triggers a breakpoint exactly as you want.

Consider this example. It is a recursive implementation of the mathematical function: Factorial. On a 32 bit system it will overflow when called with a sufficient high value. The example shows how to a breakpoint when an overflow occurs.

    staticvoid ConditionalBreakpointExample(Args args)
    {
        int factorial(int _value)
        {
            int result = 1;
            int prevResult;
            int i;
        
            for (i=1; i<=_value; i++)
            {
                result = result * i;
            
                if (result < prevResult)
                {
                    breakpoint; //Overflow occurred
                }
prevResult = result; } return result; } factorial(maxInt()); }

The same approach can be used in many other scenarios too. Here is another example – I’d like a breakpoint to fire when a Released Product with Item Id “1000” is inserted in the database. To achieve that I add the following to the insert method on the InventTable table.

if (this.ItemId == "1000")
    {
        breakpoint;
    }

There is another way to achieve the same result. You can also insert a no-op line, like i=i; and set a normal breakpoint on that line using F9. This approach has the benefit that only the user setting the breakpoint will hit it – but at the risk of leaving some “silly looking code” behind. In contrast the breakpoint statement triggers a best practice error – so they are easy to clean out again.

Bonus trick

There is another usage of the breakpoint statement. Sometimes regular breakpoints in forms don’t fire – here the breakpoint statement also comes in handy. Instead of setting a breakpoint using F9 – just type “breakpoint;” in the code. This will trigger the debugger.

This post is provided AS-IS, and confers no rights or warranties.

X   Debugging banner 2


Viewing all articles
Browse latest Browse all 550

Trending Articles