Monday, March 18, 2013

Very cool! Conditional breakpoints in the SSJS Debugger

I blogged about the new SSJS Debugger in Designer 9 and how to use it to find bugs in your java script code some weeks ago. And today i want do show you how to use conditional breakpoints in the new debugger.

Every developer knows that it is very annoying when you have a problem in a loop which occurs only in a particular iteration. So you have to step through the loop many times only to get to the faulty iteration. And when you click step over one time to often you can start from the beginning. Look at the following example:

var view=session.getCurrentDatabase().getView("Employees");
var doc=view.getFirstDocument();
var money=0;
while(doc!=null){
 money=money+100/doc.getItemValueDouble("value");
 doc=view.getNextDocument(doc);
}
getComponent("result").setValue(money);

We are looping through a view and make a litte calculation. When we run this example we get a value of "inifinty". So we are pretty sure that on one iteration there is a Division by zero. But the big question is which document contains the zero value? So we can step through the whole loop to find the doc which contains a zero vale or define a conditional breakpoint.

1. Go to the source tab of your xPage.
2. Left click the breakpoint area left of the line numbers to define a breakpoint.
3. Right click the new breakpoint and select "Breakpoint Properties"

4. Define the condition when your breakpoint should stop the script execution



After the condition is set the SSJS Debugger will stop the script execution every time the code where the break point is defined is executed and the condition is true. In our example stops the execution when doc.getItemValueDouble("value") is 0. When the Execution stops you can find out with the expressions view which document is causing the error. For example you can get with doc.getUniversalID() the UNID from the document with the 0 value.

Condtional breakpoints are a very valuable tool to find bugs in complex SSJS code.

Reference to all my posts about the SSJS Debugger

No comments:

Post a Comment

ad