Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Your First Apex Code and Debug Logs

Write and run your first Apex with Execute Anonymous, print values with System.debug, read debug logs and understand log levels, with Java stand-ins.

The fastest way to try Apex is anonymous Apex: a snippet of code that runs once, with no class around it, and is thrown away afterwards. Developers use it to test ideas, create sample data and run quick fixes. In this lesson you write your first snippet and learn to read the debug log, the main way to see what your code did.

Execute Anonymous

In the Developer Console choose Debug, then Open Execute Anonymous Window (or press Ctrl+E). Type code, tick Open Log, and click Execute. In VS Code run the command SFDX: Execute Anonymous Apex with Currently Selected Text.

Java
String name = 'Ada';
Integer year = 2026;
System.debug('Hello, ' + name + '! The year is ' + year);

There is no main method and no class. Statements run top to bottom. System.debug() writes a line to the log. This is Apex's equivalent of System.out.println, with one important difference: output never appears on a screen, only in the log.

Anything can be a statement

You can use variables, loops, if statements and even database operations in anonymous Apex:

Java
List<Integer> squares = new List<Integer>();
for (Integer i = 1; i <= 5; i++) {
    squares.add(i * i);
}
System.debug('Squares: ' + squares);

The log will contain a line like USER_DEBUG|[5]|DEBUG|Squares: (1, 4, 9, 16, 25). Apex prints lists in parentheses.

Here is a Java stand-in doing the same work (Java prints lists in square brackets, Apex in parentheses), with a tiny helper that imitates the log line format. The real log has a timestamp and extra columns; this keeps just the essentials.

Java Stand-in (Java): anonymous snippet and a debug helper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Reading a debug log

A real debug log is long even for tiny code, because it records every step. Open it in the Developer Console and tick Debug Only to see just your System.debug lines. Each line has an event type such as:

Event Meaning
EXECUTION_STARTED / EXECUTION_FINISHED Start and end of the transaction
CODE_UNIT_STARTED A class, trigger or flow started
USER_DEBUG Your System.debug output
SOQL_EXECUTE_BEGIN A database query began (with its text)
DML_BEGIN An insert, update or delete started
EXCEPTION_THROWN An error was raised
LIMIT_USAGE_FOR_NS Governor limit summary at the end

The last row is gold: at the end of a log you can see how many queries you ran against the limit of 100, so you notice trouble before users do.

Log levels

You can choose what the log records through a trace flag, which links a user to a debug level. Levels run from least to most verbose: NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, FINEST. Each System.debug call can pass its own level:

Java
System.debug(LoggingLevel.ERROR, 'Something went wrong');
System.debug(LoggingLevel.INFO, 'Just so you know');
System.debug('This is DEBUG level by default');

A line is written only if its level is at least as important as the level set for the log category. Logging at FINEST slows a transaction and fills logs fast (logs are capped at 20 MB), so raise detail only while investigating.

Here is a stand-in that models log filtering.

Java Stand-in (Java): filtering by log level

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Debugging habits

  • Print variables, not just messages: System.debug('amount=' + amount) beats System.debug('here').
  • Use JSON.serializePretty(obj) to print a whole object or record neatly.
  • Do not leave noisy debug calls in code that runs per record; they cost CPU time.
  • Use the Apex Replay Debugger in VS Code for stepping through a saved log line by line when prints are not enough.

Common mistakes

  1. Forgetting that the log is a separate screen. If nothing appears, check that Open Log is ticked or open the Logs tab.
  2. Using double quotes around strings. Apex needs 'single quotes'.
  3. Forgetting the semicolon at the end of a statement.
  4. Running anonymous code in production, where it could change real data. Practise in a Developer Edition org.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does System.debug output go?

    System.debug writes to the debug log, which you read in the Developer Console or VS Code.
  2. 2. How does Apex print a list of 1, 4 and 9?

    Apex's default list formatting uses parentheses, for example (1, 4, 9).
  3. 3. What does the LIMIT_USAGE_FOR_NS section tell you?

    It summarises usage such as SOQL queries and DML statements against the transaction limits.
  4. 4. Which log level is the most verbose?

    FINEST records the most detail. ERROR records the least.

Summary

  • Anonymous Apex runs a snippet once, with no class needed.
  • System.debug writes to the debug log; lists print in parentheses.
  • Logs record events such as queries, DML and exceptions, and end with a limits summary.
  • Log levels filter detail from ERROR up to FINEST; use high levels sparingly.

Next: Execution Model and Governor Limits