You can directly run Apex code in the Salesforce Developer Console primarily through its Execute Anonymous window. This powerful feature allows developers to quickly test Apex snippets, debug logic, and perform data manipulation without needing to deploy a full class or trigger.
What is the Developer Console?
The Developer Console is an integrated development environment (IDE) provided by Salesforce, accessible directly from your browser. It offers a suite of tools for developing, debugging, and testing Apex code, Visualforce pages, Lightning components, and more. It's an indispensable tool for Salesforce administrators and developers alike.
Running Apex Code Using the Execute Anonymous Window
The most common method to run arbitrary Apex code in the Developer Console is by using the Execute Anonymous window. This is ideal for testing specific lines of code, verifying data changes, or executing one-off scripts.
Here’s a step-by-step guide:
-
Open the Developer Console:
- From any Salesforce page, click the Gear Icon (
) in the top-right corner.
- Select Developer Console from the dropdown menu. This will open the Developer Console in a new browser window or tab.
- From any Salesforce page, click the Gear Icon (
-
Access the Execute Anonymous Window:
- Once in the Developer Console, navigate to the top menu bar.
- Click on Debug.
- From the dropdown, select Open Execute Anonymous Window.
- An "Enter Apex Code" window will appear.
-
Enter Your Apex Code:
-
In the "Enter Apex Code" window, type or paste the Apex code you wish to execute. You can include any valid Apex statements, such as DML operations, SOQL queries, method calls from existing Apex classes, or variable assignments.
-
Example Code Snippet:
Account acc = new Account(Name = 'Test Account from Console', Industry = 'Technology'); insert acc; System.debug('New Account Created: ' + acc.Id + ' - ' + acc.Name); // You can also call methods from existing Apex Classes or Controller Extensions: // MyUtilityClass.doSomethingUseful(); // new MyControllerExtension(null).performAction();
-
If you only want to execute a specific portion of the code, highlight that section before proceeding.
-
-
Execute the Code:
- After entering your code, click on the Execute button at the bottom right of the window.
- Alternatively, if you highlighted specific code, click Execute Highlighted.
-
Review the Debug Log:
- After execution, a new entry will appear in the Logs pane at the bottom of the Developer Console.
- Open the Debug Log when it will appear in the Logs pane. Double-click on the log entry to open it in a new tab within the Developer Console.
- In the log, you can filter for
USER_DEBUG
messages to see the output of yourSystem.debug()
statements, or examine the full execution flow, limits, and system information to troubleshoot any issues.
What Kind of Apex Code Can You Run?
The Execute Anonymous window is incredibly versatile for testing various Apex functionalities:
- Data Manipulation Language (DML) Operations: Create, update, or delete records (e.g.,
insert
,update
,delete
). - SOQL and SOSL Queries: Test queries to fetch data from your Salesforce org.
- Method Calls: Invoke methods from existing Apex classes, including utility classes or controller extensions. This is particularly useful for testing business logic developed in
Apex Class Code
or code supportingVF Page Code
. - Variable Assignments and Loops: Test complex logic flows.
- Exception Handling: Simulate and test
try-catch
blocks.
Practical Insights and Tips
-
Use
System.debug()
Extensively: This is your primary tool for understanding code execution in the anonymous window. Output variable values, messages, and progress markers. -
Check Debug Logs: Always review the generated debug logs. They provide crucial information about code execution, governor limits, and errors.
-
Be Mindful of Data: When performing DML operations, remember that these changes are committed to your Salesforce org. If you're experimenting with sensitive data, consider working in a Sandbox environment or using
Database.rollback()
within atry-catch
block for transactional integrity. -
Testing
VF Page Code
orApex Class Code
: While you don't run entire Visualforce pages or Apex classes in the Execute Anonymous window, you can instantiate objects of your Apex classes (especially controller extensions) and call their methods to test their logic. For example:// Assume you have an Apex Class named 'MyAccountController' // or a Controller Extension for a VF page. public class MyAccountController { public String someProperty { get; set; } public MyAccountController() { someProperty = 'Initial Value'; } public PageReference saveAccount() { // Logic to save an account System.debug('Saving account logic executed!'); return null; // or a PageReference } } // To test this in Execute Anonymous: MyAccountController controller = new MyAccountController(); System.debug('Property before: ' + controller.someProperty); controller.someProperty = 'Updated Value'; controller.saveAccount(); System.debug('Property after: ' + controller.someProperty);
Comparison of Execution Methods
While the Execute Anonymous window is excellent for quick testing, it's just one way Apex runs.
Method | Purpose | Use Case |
---|---|---|
Execute Anonymous | Immediate execution of arbitrary Apex snippets. | Rapid prototyping, debugging, one-off data scripts. |
Apex Classes | Encapsulate application logic for reuse. | Backend for Visualforce pages, Lightning components, APIs. |
Apex Triggers | Execute logic automatically before or after DML. | Enforcing business rules, data validation, automation. |
Visualforce Pages | Render user interfaces with Apex as a controller. | Custom UI, complex forms, PDF generation. |
Batch Apex | Process large volumes of records asynchronously. | Data cleansing, bulk updates. |
Schedulable Apex | Execute Apex code at specific times. | Nightly data syncs, recurring reports. |
For more detailed information on developing with Apex, refer to the official Apex Developer Guide.