Here is how I debug my issues. In most cases I would only make it to step 2 before I solve my problem.
Step 1: Explain it to your Rubber Duck
I try explain what my code is doing to my rubber duck. A lot of the time I figure out the problem when I'm explaining the code to someone else. (If exceptions are involved, take a look at your code at the line numbers reported and consider what could have generated the exception.)
Step 2: System Debugs
Debug log recording in Setup is turned on for your User via: Setup > Monitoring > Debug logs
. See here for more information.
I would place temporary debug statements such as System.Debug('>>>> the value of x is ' + x);
within my code to make sure that the code is executing the way I think its working. Individual SObjects, Maps of SObjects and Lists of SObjects can all be appended and that shows you all populated SObject fields.
The >>>>
is a unique string that usually doesn't appear anywhere else in the log files. This allows me to quickly find my debug output. (Logs are truncated after 2M bytes of output - there are work-arounds for this.)
Step 3: Use the Developer Console
The Developer Console is a great tool for debugging. You do the following things (and more) with the developer console
- View Logs. This is another way of viewing debug output.
- Execute SOQL. This can be used to verify that the SOQL in your code is returning the correct information
- Execute Anonymous. Apex code can be run directly from the dev console
- Checkpoints can be added in the code (maximum of 5 per class), which will allow you to stop the code executing and to see variable information.
See Josh Kaplans YouTube video for more info on the dev console
Step 4: Create a Unit Test
A unit test is a great way to figure out what is going on with a piece of code. It allows you to:
- Execute your code in an environment with no other data
- Create test data that you can use over and over again.
- Use asserts to check your code e.g.
System.assert(contacts.size() > 0);
or System.assertEquals(expectedX, actualX);
Step 5: Take a break
I find myself getting frustrated when I have been looking at the same problem for a long period of time. This does not help solve the problem, it can actually make it harder to clearly see what is going on in the code. Take a break for a while and you may find that when you look at the problem again it makes more sense.
Step 6: Ask for help
If you have done all the steps above and reached this point then you most likely need help. This is where a colleague or stack exchange come in. Make sure when asking a question that you clearly state your problem, provide enough information to make it understandable to others and if you are providing a code sample make sure that it is formatted and easy to read.
Jason - Lightning Components for Community Cloud are coming (they will be GA...SAFE HARBOR APPLIES) in the Winter '16 release. You may have a prerelease org on hand, so the way you will build and surface them is slightly different than you may be used to:
- Create a Lightning component and be sure it implements the forceCommunity:availableForAllPageTypes interface: <aura:component implements="forceCommunity:availableForAllPageTypes"/>
- Get into the Community Builder and upgrade the template to Winter 16 under the settings pane.
- Get into a template-driven community, open the Community Builder, and navigate to the page you want to edit (Winter 16 and above). Find your component in the left side palette and drag it onto the page. Publish.
So when you get your sandbox upgraded, or when you get the release, have at it!
Best Answer
General Tools and settings:
Techniques for most common use cases (be sure that Dev Tools in Chrome are open while testing):
check certain JavaScript code is called at all:
use
console.log()
,console.debug()
,console.warn()
,console.error()
(different color visualizations) -> can output a variable value as wellcheck for invalid values only:
rather always log to the console you can do
console.assert(myVar != 0)
to only show an error when assertion failsevaluate variables / context on a certain JavaScript code:
use
debugger;
(this will create a breakpoint in code execution) -> now use the console from Dev Tools while breakpoint is active.You can write JavaScript to test value of variables, do function calls and check return types, overwrite JavaScript values, and so on. This is super powerful also developing your code
output the value of a more complex Object:
while code execution is paused on a breakpoint (see above) use
copy(JSON.stringify(myObjectToEvaluate));
in the Dev Tool console - this will copy the value as JSON to your clipboard and you can process/inspect text based valuemeasure code execution time between two dedicated "anchors": use
console.time("identifier"); [code to execute] console.timeEnd("identifier");
check why code can not be saved or application is not loading at all: use the Salesforce Lightning CLI to lint your JavaScript code (very useful to check against faulty structure like missing braces)
investigate runtime exceptions origin:
sometimes runtime exceptions show up you don't know exactly where they come from. Use "Pause on Exception"-Button under the "Sources"-Tab in Dev Tools - inside the panel "Call Stack" check if you can identify classes from your code.
As code execution is paused you have the same behavior as a manual breakpoint: on every entry you are in the right JavaScript context so you can evaluate all values at that certain code execution time. Add some additional breakpoints (by clicking the line number in the Dev Tools) and you will always pause the code on certain area (like to check what values are before the exception raises or pause on code you can't write a debugger-statement by yourself)
mock data returned from you Apex controller: use the Lightning Components Inspector "Action" Tab and overwrite the result coming back from the server the next time
For sure there are much more useful tools and scenarios, so it is definitely worse checking what Chrome Dev Tools can do for you. Also what scenarios Lightning Component Inspector is used for.