Saturday, March 2, 2013

Using the Developer Console:


Using the Developer Console:
The Developer Console lets you execute Apex code statements. It also lets you execute Apex methods within an Apex class or object. In this you open the Developer Console, execute some basic Apex statements, and toggle a few log settings.
Lesson 1: Activating the Developer Console
After logging into your Salesforce environment, the screen displays the current application you’re using (in the diagram below,
it’s Warehouse), as well as your name.
1. Click your name, and then Developer Console.
The Developer Console opens in a separate window.
Note: If you don’t see the Developer Console option, you might not be using an appropriate type of Force.com
environment—see Before You Begin at the beginning of this workbook for more information.
2. If this is your first time opening the Developer Console, you can take a tour of the Developer Console features. Click Start
Tour and learn more about the Developer Console.
You can open the Developer Console at any time, and activate it again by following this lesson.
Lesson 2: Using the Developer Console to Execute Apex Code
The Developer Console can look overwhelming, but it’s just a collection of tools that help you work with code. In this lesson,
you’ll execute Apex code and view the results in a System Log view. The System Log view is the Developer Console tool you’ll
use most often.
1. Click in the Execute field at the top of the Developer Console.
2. In the Enter Apex Code window, enter the following text: System.debug( 'Hello World' );
3. Deselect Open Log and click Execute.

Chapter 1: Orientation

Every time you execute code, a log is created and listed in the Logs panel.
Double-click a log to open it in a System Log view. You can open multiple logs at a time to compare results.
System Log view is a context-sensitive execution viewer that shows the source of an operation, what triggered that operation,
and what occurred afterward. You can use this view to inspect debug logs that include database events, Apex processing,
workflow, and validation logic.
Within a System Log view, you’ll use the Execution Log panel a lot. It displays the stream of events that occur when code
executes. Even a single statement generates a lot of events. The System Log captures many event types: method entry and
exit, database and web service interactions, and resource limits. The event type USER_DEBUG, indicates the execution of a
System.debug() statement.
5
Chapter 1: Orientation
1. Click in the Execute field and enter the following code:
System.debug( 'Hello World' );
System.debug( System.now() );
System.debug( System.now() + 10 );
2. Check Open Log and click Execute.
3. In the Execution Log panel, click the Executable checkbox. This limits the display to only those items that represent
executed statements. For example, it filters out the cumulative limits.
4. To filter the list to show only USER_DEBUG events, enter: USER in the Filter field.
Note: The filter text is case sensitive.
Congratulations—you have successfully executed code on the Force.com platform and viewed the results! You’ll learn more
about the Developer Console tools in later tutorials.
6
Chapter 1: Orientation
Tell Me More...
Help Link in the Developer Console
To learn more about a particular aspect of the Developer Console, click the Help link in the Developer Console.
Anonymous Blocks
The Developer Console allows you to execute code statements on the fly. You can quickly evaluate the results in the
Logs panel. The code that you execute in the Developer Console is referred to as an anonymous block. Anonymous
blocks run as the current user and can fail to compile if the code violates the user's object- and field-level permissions.
Note that this not the case for Apex classes and triggers. You’ll learn more about the security context of classes and
triggers in the summary of Tutorial #7: Classes, Interfaces and Properties.
Summary
To execute Apex code and view the results of the execution, use the Developer Console. The detailed execution results include
not only the output generated by the code, but also events that occur along the execution path. Such events include the results
of calling another piece of code and interactions with the database.
Tutorial #3: Creating Sample Data
Prerequisites:
• Tutorial #1: Creating Warehouse Custom Objects
Some tutorials in this workbook assume you already have sample data in your database. To be able to execute the examples,
you first need to create some sample records.
Use the Developer Console to populate the sample objects you created in Tutorial 1.
1. Start the Developer Console, then click in the input field next to Execute to display the Enter Apex Code window.
2. If you installed the package in Tutorial 1, execute the following code:
ApexWorkbook.loadData();
If you manually created your schema, copy, paste, and execute the code from the following gist URL:
https://gist.github.com/1886593
3. Once the code executes, close the console.
Tutorial #4: Creating and Instantiating Classes
Apex is an object-oriented programming language, and much of the Apex you write will be contained in classes, sometimes
referred to as blueprints or templates for objects. In this tutorial you’ll create a simple class with two methods, and then execute
them from the Developer Console.
Lesson 1: Creating an Apex Class Using the Developer Console
To create a Apex classes in the Developer Console:
7
Chapter 1: Orientation
1. Click Your Name > Developer Console to open the Developer Console.
2. Click the Repository tab.
The Setup Entity Type panel lists the different items you can view and edit in the Developer Console.
3. Click Classes, and then click New.
4. Enter HelloWorld for the name of the new class and click OK.
5. A new empty HelloWorld class is created in the lower half of the Developer Console. Add a static method to the class
by adding the following text between the braces:
public static void sayYou() {
System.debug( 'You' );
}
6. Add an instance method by adding the following text just before the final closing brace:
public void sayMe() {
System.debug( 'Me' );
}
7. Click Save.
8
Chapter 1: Orientation
Tell Me More...
• You’ve created a class called HelloWorld with a static method sayYou() and an instance method sayMe(). Looking
at the definition of the methods, you’ll see that they call another class, System, invoking the method debug() on that
class, which will output strings.
• If you invoke the sayYou() method of your class, it invokes the debug() method of the System class, and you see the
output.
• The Developer Console validates your code in the background to ensure that the code is syntactically correct and compiles
successfully. Making mistakes is inevitable, such as typos in your code. If you make a mistake in your code, errors appear
in the Problems pane and an exclamation mark is added next to the pane heading: Problems!.
• Expand the Problems panel to see a list of errors. Clicking on an error takes you to the line of code where this error is
found. For example, the following shows the error that appears after you omit the closing parenthesis at the end of the
System.debug statement.
Re-add the closing parenthesis and notice that the error goes away.
Lesson 2: Calling a Class Method
Now that you’ve created the HelloWorld class, follow these steps to call its methods.
1. Execute the following code in the Developer Console to call the HelloWorld class's static method. (See Tutorial 2 if
you've forgotten how to do this). You might have to delete any existing code in the entry panel. Notice that to call a static
method, you don’t have to create an instance of the class.
HelloWorld.sayYou();
2. Open the resulting log.
3. Set the filters to show USER_DEBUG events. (Also covered in Tutorial 2). “You” appears in the log:
9
Chapter 1: Orientation
4. Now execute the following code to call the HelloWorld class's instance method. Notice that to call an instance method,
you first have to create an instance of the HelloWorld class.
HelloWorld hw = new HelloWorld();
hw.sayMe();
5. Open the resulting log and set the filters.
6. “Me” appears in the Details column. This code creates an instance of the HelloWorld class, and assigns it to a variable
called hw. It then calls the sayMe() method on that instance.
7. Clear the filters on both logs, and compare the two execution logs. The most obvious differences are related to creating
the HelloWorld instance and assigning it to the variable hw. Do you see any other differences?
Congratulations—you have now successfully created and executed new code on the Force.com platform!
Lesson 3: Creating an Apex Class Using the Salesforce User Interface
You can also create an Apex class in the Salesforce user interface.
1. Click Your Name > Setup > Develop > Apex Classes.
2. Click New.
3. In the editor pane, enter the following code:
public class MessageMaker {
}
4. Click Quick Save. You could have clicked Save instead, but that closes the class editor and returns you to the Apex Classes
list. Quick Save saves the Apex code, making it available to be executed, yet it also lets you continue editing—making it
easier to add to and modify the code.
5. Add the following code to the class:
public static string helloMessage() {
return('You say "Goodbye," I say "Hello"');
}
10
Chapter 1: Orientation
6. Click Save.
You can also view the class you’ve just created in the Developer Console and edit it.
1. In the Developer Console, click the Repository tab.
2. In the Setup Entity Type panel, click Classes, and then double-click MessageMaker from the Entities panel.
The MessageMaker class displays in the source code editor. You can edit the code there by typing directly in the editor
and saving the class.
Summary
In this tutorial you learned how to create and list Apex classes. The classes and methods you create can be called from the
Developer Console, as well as from other classes and code that you write.
Tell Me More...
• Alternatively, you can use the Force.com IDE to create and execute Apex code. For more information, search for “Force.com
IDE” on the Developer Force site: http://developer.force.com/.






No comments:

Post a Comment