| | |

Salesforce Developer Quick Tip: How to Run Anonymous Apex in Visual Studio Code


As an Apex developer, you often need to quickly test a class or verify logic without going through the full process of writing a formal test class (especially if you aren’t strictly following Test-Driven Development).

In the Developer Console, this is easy—you just open the “Execute Anonymous” window. But if you are working in Visual Studio Code (VS Code), the process is slightly different.

In this Salesforce Quick Tip, we’ll figure out exactly how to set up and execute Anonymous Apex scripts directly within your IDE.

Step 1: Locate the Scripts Folder

In your VS Code project structure (File Explorer), scroll down to the bottom of the list. You should see a default folder named scripts. Inside that, there is usually a subfolder named apex.

Step 2: Create Your Script File

  1. Right-click on the apex folder.
  2. Create a new file.
  3. Name it whatever you like (e.g., funScript.apex), but ensure it ends with the .apex extension.

Step 3: Write Your Code

Open your new .apex file and write the code you want to test. For example, if you have a class named SuperFunClass with a method funThingsShouldHappen(), you would instantiate it just like standard Apex:

Apex

SuperFunClass demo = new SuperFunClass();
demo.funThingsShouldHappen();

Step 4: Execute the Script

There are two easy ways to run this code:

  1. The “Execute” Button: In the .apex file editor, you will see a small “Execute” text button (usually known as Code Lens) appearing above your code. simply click it.
  2. Command Palette: Press Ctrl + Shift + P (or Cmd + Shift + P on Mac) and search for the command:SFDX: Execute Anonymous Apex with Editor Contents

The Result

Once executed, the output terminal will open automatically. You can scan the logs for your System.debug statements to verify your code is working exactly as expected.


For more development tutorials and Salesforce quick tips, check out the Coding With The Force YouTube channel.

Need help with your Salesforce Org?

If you need help with your Salesforce org, schedule an hour of consulting time with me! I’m one of only ~500 Salesforce Certified Technical Architect’s (CTA) worldwide and I’ve spent over 30,000 hours building Salesforce implementations over the last decade!

Schedule and hour of consulting with me here!


Do you want to be the next Salesforce Certified Technical Architect (CTA)?

If you need training to help you on your journey to complete your Salesforce CTA Board then why not sign up for the cheapest Salesforce CTA course out there, with someone who has training over a million Salesforce professionals worldwide! You can check out and enroll in the course below!

Sign up for the CTA course here!

Or if a course isn’t your thing, you can always sign up for an hour long CTA study session with me here:
Schedule 1-on-1 CTA Coaching Here!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture


AI DISCLAIMER: Gemini assisted me in writing this blog post by analyzing and summarizing the contents on the YouTube video I created that is linked at the top of this post.

Similar Posts