The Apex Master Class

What Is an Apex Class? (Apex Master Class Ep. 7)

4 min read

Subscribe on YouTube

What Is A Class?

Welcome back! Now that you’ve got an IDE set up, it’s time to actually make something. Today: what an Apex class is, and how to create one.

Let’s start by dropping the word “Apex” entirely, because it’s doing nothing for us here. Whether you’re writing Apex, Java, C# or any other object oriented language, a class means the same thing.

The best description I’ve heard is that a class is a blueprint for creating objects.

Now, in Salesforce that word “object” is unhelpfully overloaded, because we also call our database tables objects. Account is an object. Contact is an object. That’s a different meaning of the word, and it trips up almost everybody at this stage. So let’s go with something clearer.


The Toaster

Imagine you want to write the software that runs a toaster. What does a toaster need to do? Set a timer. Set a heat level. Stop.

There are thousands of toaster models in the world made by all sorts of companies. But the concept of a toaster is the same across all of them. So you write one blueprint that could work for any of them:

Apex
public class Toaster
{
    public void setToasterTimer(Integer minutes)
    {
        //logic to set the timer goes here
    }

    public void setHeatLevel(Integer level)
    {
        //logic to set the heat level goes here
    }

    public void stopToaster()
    {
        //logic to stop the toaster goes here
    }
}

That’s a class. It’s a blueprint that describes what a toaster is and what it can do. It isn’t a toaster itself, in the same way that a blueprint for a house isn’t a house.


It Doesn’t Have To Be A Physical Thing

Here’s the bit I don’t love about the standard “a class represents an object” line: it makes people think they need to model physical things. And then they quite reasonably say “I’m never going to build a toaster in Salesforce.”

Fair enough. So widen it: a class is a blueprint for a thing or a service.

A class can absolutely represent a service you’re providing to your users. In one of my earlier tutorials I built a mapping application that plots records on a Google Map. The class behind it is a blueprint for that mapping service, and it works no matter what you point it at. Map Leads with it. Map Contacts with it. Same blueprint.

So when you sit down to write a class, ask yourself: what thing or what service am I describing here? That question will steer you right most of the time. You’ll see this idea again and again once we get to separation of concerns later on.


Creating A Class In VS Code

Assuming you’ve got your project set up from the earlier episodes, this takes about ten seconds.

1. Hit Ctrl+Shift+P to open the command palette. (Remember this hotkey. You’ll use it constantly.)
2. Type SFDX: Create Apex Class and select it.
3. Type your class name. Let’s use MappingService.
4. Accept the default directory, force-app/main/default/classes. Unless your org uses a custom folder structure, and most don’t, this is what you want.

VS Code generates two files for you: MappingService.cls and MappingService.cls-meta.xml. That second file is metadata Salesforce needs (mostly the API version); you can safely ignore it for now.

Hit Ctrl+S and it deploys straight to your org.


Creating A Class In IntelliJ

If you went the IntelliJ and Illuminated Cloud 2 route (my personal preference), it’s even quicker: right click the classes folder, choose New > Apex Class, name it, done. Ctrl+S saves it to your org.


Checking It Actually Worked

Saving from an IDE deploys to your org immediately, which feels like magic the first few times. Let’s verify it.

Log into your org, click the gear icon in the top right, go to Setup, then search for Apex Classes in the quick find box. Your new class should be sitting right there.

You can also see it in the Developer Console (gear icon > Developer Console, then File > Open). Same class, different window.

Once you’ve confirmed it’s there, close the Developer Console and go back to your IDE. It’s a fine place to look at things, and a rough place to build things.


What’s Next

So a class is a blueprint for a thing or a service, and you can now create one in about ten seconds from either IDE. If you want to go deeper on what classes can do, the Apex Developer Guide has a full chapter on defining classes.

Right now our MappingService class is completely empty, which is not tremendously useful. In the next episode we’ll start filling it in, beginning with variables.

See you next time!


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