Let's make a new class named codeClass. For this, press Command + Shift + P on macOS or Control + Shift + P on Windows and select SFDX: Create Apex Class

Write your class name codeClass

Select where you want to save the file. To maintain our file structure, we always save with the rest of our Apex classes in force-app/main/default/classes directory.

Now in our editor we have our class file open! You'll also notice there were two files made.

  • codeClass.cls: The actual class file where we will be writing our code
  • codeClass.cls-meta.xml: An XML file that contains information about our code, like API Version and Status.

In our codeClass.cls file, let's write a method with a simple System.debug

public class codeClass{
    public static void outputMethod(){
        System.debug('This is my debug statement');
    }
}

Before we run this code, we need to deploy this to our org. There are two ways to do this:

To deploy one file at a time, with the file open in the editor, press Command + Shift + P on macOS or Control + Shift + P on Windows and select SFDX: Deploy This Source to Org and press enter.

To deploy the entire folder, right click on the classes folder, and select SFDX: Deploy source to org

To use the equivalent of Anonymous Execution in VSCode, write your code at the end of your class file, select it and choose SFDX: Execute Anonymous Apex with Currently Selected Text. Please remember not to deploy this code to productin because it's technically an error and VSCode wouldn't allow you to upload the code anyways.

To see our output statement, select the Output tab and you'll find the This is my debug statement in the log!

Another way to do this, is to create an empty class file outside the folder and never deploy it to org, and execute code from the class file.

Chatper 6: Writing and Executing SOQL