Beginner 15 min read

Execute Anonymous

By the end of this lesson, you'll be able to:

  • Run a snippet of Apex without creating a saved class
  • Explain when Execute Anonymous is and is not the right tool
  • Use Execute Anonymous to inspect the result of a simple expression

Prerequisites: "Developer Console"

What makes it "anonymous"

An "anonymous" block isn't attached to any named class — it exists only for the moment you run it, then disappears. That's precisely why it's fast: no saving, no deploying, no leftover file to clean up.

You already used this in the first lesson's exercise. This lesson goes a little deeper into when it's the right tool.

When to reach for it — and when not to

Use Execute Anonymous for:

  • Quickly checking what a line of Apex actually returns
  • Running a one-off data fix (updating a handful of records directly)
  • Testing a method on a class you've already saved, without writing a full test class yet

Don't use it for anything that needs to run automatically or repeatedly — that's what triggers, scheduled Apex, and real classes are for. Execute Anonymous code is never saved anywhere; if you close the window, it's gone.

Trade-offs and when NOT to use this

Because nothing in an anonymous block is saved, tested, or version-controlled, it's inherently a scratchpad tool — never a place to build real functionality. Production logic belongs in properly saved, tested classes and triggers, which the rest of this course focuses on.

Exercise

In Execute Anonymous, debug the result of 5 + 3 * 2, then debug today's date using Date.today().

Show hint

System.debug() works on any expression, not just strings — try System.debug(5 + 3 * 2);

APEX

Execute Anonymous Quiz

1. What happens to code run in Execute Anonymous after the window is closed?

2. Execute Anonymous is a good place to build permanent business logic.

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Execute Anonymous runs a throwaway block of Apex immediately, without saving it as a class — the fastest way to test a small idea, but not a substitute for real, version-controlled, tested code.