Free Online Apex Playground
Write Apex code and run it in your browser, with System.debug output in a console and SOQL and DML against sample data. A learning subset of Apex, with no Salesforce org needed.
Press Run, or Ctrl or Cmd + Enter, to run the file. This is a learning subset of Apex run by our own interpreter, not Salesforce. The sample data is invented and is created fresh on every run, and Salesforce governor limits are not enforced. Statements at the top level run in order, and classes share the same file.
Preview
Console
Output appears here. Press Run, or Ctrl or Cmd + Enter.
How to use
Write Apex in the editor, or start from the example. Statements at the top level run in order. Classes, interfaces and enums can sit in the same file, before or after the statements that use them.
Press Run, or Ctrl or Cmd + Enter. The Console shows each System.debug line, with its level (DEBUG, INFO, WARN or ERROR). If an exception is not caught, it prints like Salesforce does, with the stack, and the run stops.
Query the sample data with [SELECT ...] and bind variables such as :accountId, and change it with insert, update, upsert, delete and undelete, or with Database.insert and the other Database methods.
Open the preview to see the run summary and the Records that changed: a small table per object for inserted, updated (changed values are highlighted) and deleted records. The sample database is created fresh on every run.
If the code has a syntax error, the Console shows the line and column and nothing runs. If it uses something the playground does not support, such as a trigger or Test.startTest(), you get the line and a message that it is not supported in this playground.
Press Stop to cancel a run, Download to save a zip with a standalone page (your code, the output and the changed records) and your code as Main.apex, or the copy icon to copy the code.
Why use this tool
Our own Apex interpreter
A lexer, a parser and a tree-walking interpreter written for this site, with no use of eval. Your code is read as text and never run as JavaScript.
Classes, interfaces and enums
Fields, static and instance members, constructors with this() and super(), properties with get and set, inner classes, virtual, abstract and override, interfaces, and compile-style checks such as a missing override keyword.
Collections with Apex semantics
Lists, sets and maps with initialisers and array syntax, passed by reference, with Apex errors such as List index out of bounds, null for a missing map key, and case-sensitive String keys.
Numbers that behave like Apex
Integer, Long, Double and Decimal are separate types. Integer division gives a Decimal, Integer arithmetic wraps on overflow, null in arithmetic throws, and setScale and round take a RoundingMode.
Exceptions you can catch
try, catch and finally with typed catches, the built-in exception types, custom exceptions that extend Exception, and uncaught exceptions that print with the stack and stop the run.
SOQL, SOSL and DML on sample data
Inline queries with bind variables, Database.query, insert, update, upsert, delete, undelete, partial success with SaveResult, and DmlException messages in the Salesforce style.
The common built-in classes
System, Math, String, Integer, Long, Double, Decimal, Date, Datetime, Time, JSON, Limits, UserInfo, Database, Schema basics, Pattern and Matcher, and the Assert class.
A loop that never ends is stopped
The program runs in a worker. It stops after 20 million steps, or after 8 seconds, with a message, and the page stays responsive.
Clear errors
Syntax errors show the line and column. Unsupported constructs are refused with their line. Runtime problems print the exception type, message and stack.
Editor with Apex colours
Keywords, types, annotations, strings, comments and SOQL inside brackets are coloured, with line numbers, bracket matching and undo.
About this tool
Write Apex and run it in your browser. System.debug output appears in a console, and SOQL, SOSL and DML statements work against a built-in set of invented sample data (accounts, contacts, opportunities, leads, cases and more), so you can practise without a Salesforce org, a login or a sandbox. The playground has its own Apex lexer, parser and interpreter. It runs a learning subset of the language, not the Salesforce platform: it does not connect to Salesforce, and the data is made up. This site is not affiliated with or endorsed by Salesforce.
Statements at the top level of the file run in order, like Anonymous Apex, and you can declare classes, interfaces and enums in the same file and use them. Real Apex keeps every class in a file of its own, so this single file is a convenience for learning, not how a project is laid out. After a run, the preview shows a summary (steps, queries and DML statements) and, for each object, the records that were inserted, updated or deleted, so DML has a visible effect.
Because it is a subset, some things are missing on purpose: triggers, batch, queueable, scheduled and future jobs, callouts, running tests, Visualforce controllers and the governor limits. A construct the playground cannot run is refused with its line number, instead of being run in some other way. The questions below list what is supported and what differs from the real platform.
Frequently asked questions
No. It never contacts Salesforce or any other service, and you do not log in. Your code runs in your browser against sample data that this site made up. The records, ids and numbers are invented and have nothing to do with any org. You cannot run the classes of your own org here.
It is a learning subset, run by our own interpreter. It follows the language closely where it can, but it is not the Salesforce runtime, so test anything important in a sandbox. The biggest difference is that Apex is compiled: Salesforce reports a type error or an unknown variable before any code runs, while here those problems appear when the line is reached, and statements before it have already printed their output.
Real Apex puts each top-level class in its own file and has no loose statements, except in Anonymous Apex, which has statements and no classes. To keep one small editor, this playground accepts both in one file: class, interface and enum declarations are read first, and then the statements run in order. A class is only a definition until a statement uses it. Static initialisers run the first time a class is used.
Variables of Integer, Long, Double, Decimal, String, Boolean, Date, Datetime, Time, Id, Object, sObject types (Account, Contact, Opportunity, Lead, Case, User, Task and the rest of the sample data, and the generic SObject), List, Set and Map (including Integer[] syntax and initialisers). Operators: arithmetic, compound assignment, ++ and --, comparisons, == and != (Strings ignore case), === and !==, &&, ||, !, the ternary operator, ??, safe navigation ?., bitwise operators and shifts, instanceof and casts. Statements: if, for, for-each, SOQL for loops, while, do-while, break, continue, return, throw, try, catch, finally, switch on with when values, types and else, and DML. Types: classes (static and instance members, constructors, properties, initialiser blocks, inner classes), interfaces, enums, virtual, abstract and override, super and this, sharing keywords and annotations (accepted and ignored), and Comparable, Comparator and Iterator.
System (debug at every level, assert, assertEquals, assertNotEquals, now, today, currentTimeMillis), Assert, Math (abs, ceil, floor, round, roundToLong, rint, max, min, mod, pow, sqrt, cbrt, exp, log, log10, the trigonometric functions, signum, random and more), String (dozens of methods such as substring, indexOf, contains, split, replace, replaceAll, format, join, valueOf, left, right, capitalize, abbreviate, leftPad and isBlank), Integer, Long, Double and Decimal (valueOf, setScale, round, divide, pow, format and more), Boolean, Date, Datetime and Time (today, now, newInstance, addDays, addMonths, daysBetween, format with patterns, toStartOfWeek and more), the List, Set and Map methods, JSON.serialize, serializePretty and deserializeUntyped, Limits and UserInfo (informational), Schema.getGlobalDescribe with basic describe calls, Pattern and Matcher, Database.insert, update, upsert, delete, undelete, query, queryWithBinds and countQuery, SObject get, put, getSObjectType and clone, and the exception classes with getMessage, getTypeName, getCause and getStackTraceString.
Triggers, batch Apex (Database.Batchable and executeBatch), Queueable and Schedulable jobs, @future methods, callouts (Http, HttpRequest), email (Messaging), platform events (EventBus), Visualforce controllers (PageReference), @isTest classes and test methods as tests (they are accepted but never run), Test.* methods (only Test.isRunningTest(), which is false, works), System.runAs, savepoints and rollback, merge, JSON.deserialize into typed classes, describe calls beyond the basics, and the Apex used with managed packages. @AuraEnabled and similar annotations are accepted, but they have no effect. Each unsupported construct stops the run before it starts, with its line number and the message "not supported in this playground".
No. Real limits such as 100 SOQL queries and 150 DML statements per transaction, the heap size and the CPU time are not enforced. The Limits class works, and reports how many queries, query rows, DML statements and DML rows your run used, next to the usual maximums, but the numbers are only for information. The playground has its own safety limits: a run stops after 20 million steps or 8 seconds, and methods that call each other more than a few hundred levels deep (fewer when each call has loops or try blocks) stop with a stack depth error, where Salesforce allows deeper recursion.
Type and name errors appear when the line runs, not before anything runs. Partial type checking only: assignments between numbers and between classes are checked, but not every method signature. Set and Map iterate in the order you added items, while Salesforce does not promise any order. Time zone is always GMT, so Datetime.format uses GMT, and there is no user locale (dates format like 3/15/2024 and numbers like 1,234.5). Integer division gives a Decimal with up to 34 significant digits and can be assigned to an Integer, which truncates. System.debug of a Date prints a time of 00:00:00, as the debug log does. The rounding rules are HALF_EVEN for setScale and Decimal.round(), and half up for Math.round. System.assertEquals compares Strings without regard to case, like ==. The Id of new records is generated by the sample database. List.sort puts nulls first and orders Strings by character code.
The sample database has accounts, contacts, opportunities, leads, cases, users, tasks, campaigns, products and opportunity line items, with parent fields (Account.Name), child subqueries (SELECT ... FROM Contacts) and aggregates. Inline [SELECT ...] and Database.query use the same engine as the SOQL Playground, and [FIND ...] runs SOSL. A query assigned to one record gives a QueryException if it finds no rows or more than one, and a field you did not select throws a SObjectException, like the real platform. DML runs on the same data, so a query after an insert finds the new record. insert, update, delete and undelete are all-or-nothing and throw a DmlException; Database.insert with false as the second argument keeps the records that worked and gives a SaveResult for each. Changes last for the run only: the database is created again every time you press Run.
One line for each System.debug call, starting with its level: DEBUG, INFO, WARN, ERROR, or FINE, FINER and FINEST (which show as plain log lines). LoggingLevel.NONE prints nothing. Lists print as (1, 2), sets and maps as {a, b} and {a=1}, and records as Account:{Name=Acme}. An uncaught exception prints as, for example, System.NullPointerException: Attempt to de-reference a null object, followed by the stack, one frame per line with its line and column. A custom exception prints with its class name.
The program runs in a Web Worker, so the page stays responsive. A loop that goes on for more than 20 million steps stops with a message and the line, and the output written so far stays in the Console. If a single step is very slow, the worker is stopped after 8 seconds. You can also press Stop at any time. A stopped run cannot be caught by try and catch, because it is not an Apex exception.
The code is kept in this browser on this device, so it is still there when you come back. Saved code is never run when the page loads: only the built-in example runs on a first visit, and your own code runs when you press Run. After you clear the editor, the example is shown again the next time you visit. Nothing you type is sent to a server.