Friday, May 18, 2012

Debugging APEX

Posted in [Salesforce.com], [Technical] By Mark Boyer @ 10/17/2009 1:54 PM

Let's face it. Salesforce.com and Force.com are new kids on the block.  They're growing up fast, and establishing their territory, but they've still got some maturing.  Most languages these days have an interactive debugger.  However, APEX is the exception.  If you're new to APEX coding, I recommend you take a look at three things to help you overcome this hurdle: 1) Debug Logs, 2) Running tests in Eclipse, 3) Running tests in Salesforce.com.

Since there is no interactive debugging with APEX, you've got to find ways to identify how far your code ran and what it did when it got there.  One thing I do while writing code is to add debug logs to indicate the values I'm testing and to tell me that things ran up to that point.  If you write good test methods, you should know what to expect at each point in your code.

 

system.debug("checking value: " + myvar);

 

When the debugger passes this point, a statement is logged with the value and if it's not what I expected, then I know where to start digging.  It takes a little more effort during development, but pays big dividends when testing and even bigger dividends when you run into production problems.

One thing that can be a total pain is writing your code in Eclipse and then deploying it to the server each time you're ready for testing.  If your development style has you writing small blocks of code and then testing, you're going to spend a lot of time deploying the solution.  One short cut if working in a development environment is to  simply "Save" the code to the server rather than deploy it.  This can save time, but it's still time consuming. 

Once you've got your code on the server, it's pretty straightforward to run the test code or to do run through some test scenarios.  After you run the test you can then click on the code coverage percentage where you'll be able to see your code coverage in blue (covered) and red (not covered).  Still, this can take a lot of time redeploying.

Perhaps a better way is to go to Eclipse, right click on the Classes folder, and pick "Force.com->Run Tests".  This will run all the tests for your classes and return the code coverage to Eclipse in a list.  It's a lot faster and easier than going back and forth between the application.

Once you pass all of your test cases and the code runs into a problem in production, you'll want to turn on the debug log to see where things go.  If you've done a good job with debug log statements in code, the debug session should go very quickly.  If not, you'll be scratching you head and having to do a lot of interative deployments to figure out where things went wrong and sometimes this can be difficult to recreate in complex implementations on a development environment.

Adding debug logs, using the testing on the server and eclipse has saved me a lot of development time, and I hope it works equally well for you.

 

Comments

Got something to say? Join the discussion »

leave a reply

 [Quick Submit with Ctrl+Enter]

Remember my details
Notify me of followup comments via e-mail

C5_Insight

About C5 Insight

We are a Microsoft Gold Certified partner focusing on SharePoint, Microsoft Dynamics CRM and Salesforce.com.  Learn more about us by visiting our website.

Search

We Wrote the Bible on Microsoft SharePoint and Dynamics CRMBook-Microsoft Dynamics CRM 2011 and SharePoint 2010 Bible

Tags

Maximize

Recent Comments

Maximize

Blog Roll

Maximize

Disclaimer

The information herein may be used solely at your own risk.  No warranty is made by the author or by C5 Insight, Inc.

The opinons expressed herein are those of the individual authors and do not necessarily represent C5 Insight, Inc in any way.

Copyright 2011 by C5 Insight