Apex Basics: A Beginner's Guide to Salesforce Apex Programming

neelam rakeshneelam rakesh
2 min read

Introduction to Apex

If you’re new to Salesforce development, Apex is the programming language you need to learn first. It helps you add custom logic to Salesforce and automate things easily.

In this guide, we’ll start with Apex basics, understand its simple rules, and write our first Apex class step by step. Ultimately, you’ll get a small practice task to try on your own.


What is Apex?

Apex is a coding language used inside Salesforce. It’s similar to Java but works only in Salesforce. It helps in:

  • Automating things – Running code when something happens (like saving a record).

  • Adding custom logic – Making Salesforce work exactly how you want.

  • Working with data – Getting, saving, or changing records in Salesforce.

  • Connecting with other systems – Talking to outside services using APIs.


Key Features of Apex

  1. Works in Salesforce’s Cloud – Runs on Salesforce servers.

  2. Handles Salesforce Data – Can read and change Salesforce records.

  3. Follows Rules (Governor Limits) – Stops one user from using too many resources.

  4. Runs on Demand – Only runs when needed, saving resources.

  5. Supports Queries & Updates – Can get and change data using special commands.


Your First Apex Class

Now, let’s write a simple Apex class to see how it looks.

public class HelloApex {
    // A method that prints "Hello, Apex!"
    public static void sayHello() {
        System.debug('Hello, Apex!');
    }
}

Breaking it Down:

  • public class HelloApex → Creates a class (like a container for code).

  • public static void sayHello() → A method inside the class that does something.

  • System.debug('Hello, Apex!');Prints a message when the method runs.


Running Apex Code

To run this code:

  1. Open Developer Console in Salesforce.

  2. Click on Debug > Open Execute Anonymous Window.

  3. Type the command below and press Execute:

HelloApex.sayHello();
  1. Go to Logs, and you will see "Hello, Apex!" printed.

Practice Task: Write Your First Apex Class

What You Need to Do:

  • Create an Apex class called MyFirstClass.

  • Inside the class, create a method called greetUser.

  • The method should print: "Welcome to Apex, [Your Name]!"

  • Run the method using Execute Anonymous.

Example Code (Try Writing it Yourself First!):

public class MyFirstClass {
    public static void greetUser() {
        System.debug('Welcome to Apex, John!');
    }
}

To run it:

MyFirstClass.greetUser();

Next: Apex Data Types and Variables

0
Subscribe to my newsletter

Read articles from neelam rakesh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

neelam rakesh
neelam rakesh