Wednesday 29 August 2012

Creating an Apex Class Using the Developer Console

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.


To create a Apex classes in the Developer Console:
  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.
    Create a new class in the Developer Console
  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.

    Source Code editing in the Developer Console

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.

    Investigating problems in the Problems panel of the Developer Console.
    Re-add the closing parenthesis and notice that the error goes away.

1 comment: