Wednesday 29 August 2012

Using the Developer Console - Apex

The Developer Console lets you execute Apex code statements. It also lets you execute Apex methods within an Apex class or object. In this tutorial you open the Developer Console, execute some basic Apex statements, and toggle a few log settings.

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.

  1. 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.
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.
    A simple Hello World example in Apex
Every time you execute code, a log is created and listed in the Logs panel.
The Logs panel lists existing debug logs
Double-click a log to open it in a System Log view. You can open multiple logs at a time to compare results.
The 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.

The Execution Log shows a recording of events of code execution 
  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.

Filter the Execution Log to display only events of interest 
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.

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.

No comments:

Post a Comment