Debug the Problem

Let’s use the Apex Debugger to see what went wrong.
  1. Make sure that AccountViewerController.cls is open.
  2. Switch to the Debug perspective of the Force.com IDE.
  3. Make sure that the turning yellow gears (Turning gear icon) are visible in the Debug pane. If you don’t see the icon that depicts turning yellow gears, click the debug icon (Debug icon) to launch your debug configuration.
  4. Find the removeColdAccounts(List<Account> listToReduce) method.
  5. Set a breakpoint on this line:
    1Account a = listToReduce.get(i);
    AccountViewerController.cls with breakpoint in removeColdAccounts method
  6. Navigate to https://your_salesforce_instance/apex/AccountViewer.
  7. Select Hide Cold Accounts.
  8. Notice that Please Wait has been displaying for awhile. Execution has stopped because you’ve successfully hit a breakpoint.
    Visualforce page that's stopped because the breakpoint was hit
  9. In Eclipse, click the line in the Debug pane’s stack trace that corresponds to your breakpoint.
    Debug pane in the Force.com IDE, showing the stack trace
  10. Click Step Over (Step Over icon) until you’ve stepped through all the iterations of your for loop. As you do so, pay attention to the values of the variables in your Variables pane. If you notice the problem, try to correct it. If you manage to correct the problem, go ahead and skip to Fix the Problem to confirm your solution.
  11. After you’ve stepped all the way through the for loop, look at your Visualforce page. Some of the cold accounts were hidden, but at least one remains.
    Visualforce page with some but not all cold accounts hidden
  12. In this screenshot, the cold account is Flea LLC. Make a note of this account’s name. Then, let’s step through our for loop again.
  13. Deselect Hide Cold Accounts.
  14. In Eclipse, click one of the top two levels of the stack trace in the Debug pane.
    Debug pane with callout showing top two levels of stack trace
  15. To stop your debugging session, click the terminate icon (Terminate icon).
  16. To start a new session, click the debug icon (Debug icon).
  17. Wait for the turning yellow gears (Turning gear icon) to reappear in the Debug pane.
  18. Reload your Visualforce page, then select Hide Cold Accounts again.
  19. This time, pay special attention to what happens when you move past the account right before Flea LLC. Make a note of the account that you want to watch out for. In this case, the account we want to watch for is Butterfly Beauty Supplies.
  20. Click Step Over (Step Over icon) until you see that the value of a.Name is the account name you’re watching for—in this case, Butterfly Beauty Supplies.
    Debug perspective, showing in the Variables pane that the value of a is 'Butterfly Beauty Supplies'
  21. Paying close attention to the values of your variables, click Step Over until a’s value changes again.
  22. Look at the value of listToReduce. 'Butterfly Beauty Supplies' was removed, but we’ve skipped right over 'Flea LLC'. Why do you think that could be?
    Debug perspective, showing that Flea LLC was skipped
The problem lies in the value of the iterator, i. When we removed 'Butterfly Beauty Supplies' from the list, we didn’t account for the fact that removing this list item would change the positions of the subsequent items. Flea LLC is now at position 2, formerly occupied by Butterfly Beauty Supplies, but that position has already been processed. Fortunately, this problem is easily fixed.