---
schema_version: "1"
id: a198d0b6401e79eabdc6f60da9e4573c100f83cfa0072625f9c3fedbec2dbac3
title: Use Anonymous Apex
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforcedx/sfvscode/guides/programming-languages/apex/apex-anonymous.md
canonical_url: https://developer.salesforce.com/docs/platform/sfvscode-extensions/guide/apex-anonymous.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 4a74ff22ba23ecf06fd370e04b82c867113b6383c86181b770bc0a987e49b83a
products:
  - Salesforce Extensions for Visual Studio Code
  - salesforcedx
---
# Use Anonymous Apex

An anonymous block is Apex code that you can compile and run without storing its metadata.

## Create an Anonymous Apex Script

1. Open VS Code Command Palette.
2. Type **SFDX: Create Anonymous Apex Script**.
3. Enter the name of the script and the folder to save it in.
4. Modify the sample script in the text editor with your own code.
5. Save the file. By default, the file saves under the `scripts` folder.

Here's an example of anonymous Apex code that browses through all the contacts, and changes the mailing state to `New York` from `NY`. This script also logs the first and last name of the contact whose record was updated.

```JAVA
List<Contact> contList =[SELECT FirstName, LastName, MailingState, AccountId
  FROM Contact];
for (Contact cont: contList){
    if (cont.MailingState == 'NY') {
        cont.MailingState = 'New York';
        update cont;
        System.debug('Record updated for:' + cont.FirstName + ' ' + cont.LastName);
    }
}
```

## Run an Anonymous Apex Script

1. Open your script in the text editor.
2. To run the whole script, right-click in the text editor and select **SFDX: Execute Anonymous Apex with Currently Open Editor**. To run only a part of the code, highlight the code, right-click, and select **SFDX: Execute Anonymous Apex with Editor's Selected Text**. Alternatively, you can also run the previous commands from the command palette by using Ctrl+Shift+P (Windows or Linux) or ⇧⌘P (macOS).

   ![Run an anonymous script](https://developer.salesforce.com/docs/platform/media/shared/anon_exec_highlight2.png '{"class": "image-md image-framed"}')

You can also run these commands from VS Code Command Palette when the script is open in the text editor.

After you run the script, the `DEBUG CONSOLE` lists accounts and their updated ratings. To view the log in the Debug Log Viewer, click **Open Log** in the message.

![Log generated after running an anonymous Apex script](https://developer.salesforce.com/docs/platform/media/shared/anon_log2.png '{"class": "image-md image-framed"}')

The debug logs are stored under `sfdx/tools/debug/logs` in your DX project. To open it in the Log Viewer, click a log file.

![Logs are stored as shown in the explorer view](https://developer.salesforce.com/docs/platform/media/shared/stored_log.png '{"class": "image-sm image-framed"}')

## See Also

- _Apex Developer Guide_: [Anonymous Blocks](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_anonymous_block.htm)

