Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Practice in the Developer Console

Learning Objectives

After completing this unit, you’ll be able to:

  • List common data types.
  • Declare a variable.
  • Construct debug statements and explain their results.
  • Define syntax.

In the last unit, you learned about code, pseudocode, and code comments. But one thing is for sure: if you want to learn how to read and write code there’s nothing to it but to do it. So let’s get started.

Variables

Think of a variable as a container for data. You can give it a name to describe what’s in it. You can physically label it with a unique name and add things to the container. Once the container has everything you want in it, the container can be put away until it’s needed. 

Shipping boxes labeled tea, teaKettle, and spoon.

Variables are also similar to a temporary field. Like a field, each variable has a name and they also have a data type. When code runs, its variable names are not important to the compiler, the system interpreting the code. When developers try to update or fix code, however, meaningful variable names decrease the time they spend trying to understand the purpose of each variable. Consider this example:

String f = 'Melissa';

The purpose of the variable named f is not clear. A best practice is to choose clear, descriptive variable names, for example:

String firstName = 'Melissa';

While the name describes what a variable represents, the data type defines what the variable can contain.

Common Data Types

Data Type Description and Example
Integer A positive or negative number that doesn’t have a decimal point.

Integer num = 12;
Decimal A positive or negative number that has a decimal point.

Decimal num = 12.22222;
String A series of characters surrounded by single quotes. This can include any text as short as one letter to sentences.

String whatAmI = 'A String';
Boolean Typically either true or false. In Apex, null (empty) is also a valid value. Boolean is commonly used with checkboxes.

Boolean doSomething = False;
ID (Salesforce ID) Any valid 18-character Salesforce record ID.

Id contactId = '00300000003T2PGAA0';

Apex is a strongly-typed language, meaning that each time you declare (create) a variable, you set its data type, its name, and optionally, its initial value. 

Integer i = 2;

In this example, Integer is the data type, i is the variable name, and 2 is the initial value of the variable. The variable name can be whatever you would like. A best practice is to use names that clearly convey the purpose of the variable, so that your code is easy for other developers to read and understand.

When you initialize a variable (assign it an initial value), the value you assign must match the variable’s data type. If you choose not to assign a value within the variable declaration statement, the value of the variable is null.

Let’s declare a variable.

  1. In the Developer Console, click Debug | Open Execute Anonymous Window.
    Note

    Note

    Execute Anonymous runs code without saving it. It’s a great way to experiment with short snippets of code before adding the code to your application.

  2. Copy this code and paste it into the Enter Apex Code window.
    Integer numberOfSpoons;
    System.debug(numberOfSpoons);

    System.debug displays the contents of its parentheses in the debug log.

  3. Select the Open log checkbox and then click Execute. The Enter Apex Code window.

    The Execution Log opens, displaying the result of running your code.

  4. Select the Debug Only checkbox at the bottom of the window. Event:USER_DEBUG. Details: [2]|Debug|null.

    Now the Details column shows only the System.debug output:

    • The line number where System.debug is located in the executed code: 2.
    • The logging level: DEBUG.
    • The value of the numberOfSpoons variable: null.

System.debug

Imagine writing some code that just doesn’t work. There’s no one to ask and you’re one Google search short of tossing your laptop out the window. Hey, it happens to the best of us. That’s where System.debug comes in to save the day! 

System.debug(Error!);

Wouldn't it be great if you were notified every time a specific variable value changed? The System.debug statement provides that type of notification. Developers use System.debug to monitor changing values in an effort to pinpoint where their code goes awry.

  1. In the Developer Console, click Debug | Open Execute Anonymous Window.
  2. Copy this code and replace the existing code in the Enter Apex Code window with this code.
    Integer numberOfSpoons;
    System.debug(numberOfSpoons);
      
    numberOfSpoons = 1;
    System.debug(numberOfSpoons);
  3. Select the Open log checkbox and then click Execute. The Execution Log opens, displaying the result of running your code.
  4. Select the Debug Only checkbox at the bottom of the window. Did you notice anything interesting? Line 2 and line 5 are identical lines of code, but they produce different values in the log. This happens because a variable value can change at any time. Two debug log events. Line 2 value is null. Line 5 value is 1.

    We are working with a simple example here: the value of the number of spoons at two points in our code. If you were running 50 lines of code and monitoring five variables, you might want to see the name of each variable along with its value in the log details.

Developers use concatenation to connect literal text and variable values with the + sign. Concatenating information in your debug statements makes the debug log easier to read and understand.

Let's include the name of the numberOfSpoons variable along with its value in our two debug statements.

  1. In the Developer Console, click Debug | Open Execute Anonymous Window.
  2. Copy this code and paste it into the Enter Apex Code window.
    Integer numberOfSpoons;
    System.debug('The variable numberOfSpoons is: ' + numberOfSpoons);
      
    numberOfSpoons = 1;
    System.debug('The variable numberOfSpoons is: ' + numberOfSpoons);
  3. Select the Open log checkbox and then click Execute. The Execution Log opens, displaying the result of running your code.
  4. Select the Debug Only checkbox at the bottom of the window.

Two debug log events. Line 2 says “The variable numberOfSpoons is: null”. Line 5 says “The variable numberOfSpoons is: 1”.

Now it’s clear that the two values, null and 1, are values for the numberOfSpoons variable. 

Syntax

What have you noticed in these code samples about how we structure our code? The way that text and punctuation are arranged to create a programming language is called syntax. Here are some syntax rules for Apex code. 

  1. The first thing you may have noticed is that Apex statements end with a semicolon.
    String whatTimeIsIt;
    Semicolons end Apex statements in the same way that periods end sentences. If you forget the semicolon at the end of a statement, you’ll be reminded with a friendly error message.
  2. Apex strings use single quotation marks to separate literal text from surrounding code. Numbers and Boolean (true or false) values don’t need quotes.
    String whatTimeIsIt = 'It is Tea Time!';
    Integer sugarCount = 2;
    Boolean needsSugar = false;

Now you know what variables, data types, and syntax are. You learned how to declare a variable and assign a value to it. You practiced creating your own debug statements and reviewing them in the debug log. Great work!

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback