Salesforce is the world’s most popular and most successful cloud-based customer relationship management (CRM) platform. You can use it to grow your business by better understanding your customers’ needs.
One of the reasons for this success is considered to be Apex. Apex is an object-oriented programming language created by Salesforce that allows you to extend the platform’s already powerful functionality and create your own custom cloud-based software applications.
Apex is specifically designed for accessing and manipulating data. It works with Salesforce’s declarative features and is effective within a multi-tenant environment. You can access various types of metadata like custom objects and fields directly from your Apex code.
Just like any object-oriented language, like Java and C++, a big part of Apex’s functionality depends on classes. In this blog post, you’ll learn what classes are and why they are important to use. You’ll also learn about all the different components that make up a class.
What Are Apex Classes?
When you start learning a programming language, particularly an object-oriented language, there are two terms you’ll find together most of the time that can be confusing for new programmers: Classes and objects. You can think of a class as a template containing all the necessary information and instructions that you can build upon and create class instances called objects.
For example, think of a class that describes a motorcycle. A motorcycle has a model, color, horsepower, manufacturer, etc. A motorcycle object is based on this class, and you can use it to create different objects that will specify the characteristics mentioned above. In the example below, you can see a class with a few characteristics and the instantiation of three different motorcycle objects.
Components of an Apex Class
An Apex class consists of components that define an object’s functionality and characteristics. Let’s break down the structure of a class to understand it better.
When you define a class, you need to specify its level of visibility. To do this, you can use an access modifier. By default, the access modifier is private, which means that the class isn’t available to outside classes. In our example, the public keyword indicates that this class is available to other classes in the same namespace. Most class definitions are public. The last access modifier option is the global keyword. With global, your Apex class will be visible everywhere, even to classes of a different namespace.
The next thing you can specify while defining a class, although optional, is its sharing mode.
The with sharing and without sharing options are unique to Salesforce Apex classes. They indicate the sharing type for this particular class and are used as a security measure to specify record accessibility. Without sharing ignores sharing rules and is the default option for an Apex class. The virtual keyword modifier indicates that you can extend this class. Finally, the abstract keyword states that your class has methods that don’t include a body.
The last things you need to include in your class definition are the class keyword and the custom name of your class.
A variable holds a value that your programs can access to perform various types of operations. Each variable has a data type that defines what kind of values it can store. The data type specifies the variable’s size and, therefore, the memory storage it requires. Also, it determines the type of manipulations you can apply.
To use a variable, you have to declare it first. Below you can see an example of a variable declaration.
The first keyword in this example is the access modifier. It’s optional, and the concept is similar to what was mentioned earlier for the class definition. Variables are private by default, visible only to Apex code within a defining class. You must specify a variable as public or global to make it visible and accessible to other classes.
In addition to the options we mentioned for the class definition, there’s an extra modifier option for variables called protected. Only inner classes of the defining apex class and those that extend it can access a protected variable.
After the modifier, the mandatory data type keyword follows. Options include String, Integer, Boolean, sObject, and more. You can find all the Apex variables data types on the Salesforce website.
Then you would have to name your variable. Although you don’t have many limitations, you should follow the best practices. Implementing them will save you from unnecessary future problems. Lastly, you can optionally give a value to your variable when you declare it. If you choose not to, the default value is null.
A method is a block of code created within a class that contains a set of instructions and allows objects to accomplish a particular action. They’re reusable and run only when you call them. Here’s a simple method example:
The first keyword is the access modifier. The same modifiers that apply to variables apply to methods as well. Next, you need to specify the return type or the data type your method will return. It can be a primitive data type like integer or double, but it can also be a collection, an object, or even another class. If there’s no value to return, you should use the void keyword.
Then you need to name your method. A descriptive name is the best practice. Inside the parentheses, specify the data type of your input and then its name. You can separate them using commas if you have more than one input. Finally, place your method logic inside the curly brackets.
Practically, constructors are methods you can use to construct new objects based on your class and configure them, so they’re ready to use. Like with methods, you’ll have to determine a constructor’s logic and inputs. On the other hand, constructors will always return a new object; therefore, you don’t have to include a return type.
It’s important to remember that you don’t need to include a constructor in your class as they’re optional. Apex, by default, will create a constructor that will create an empty object. If you decide to include a constructor in your class, keep in mind that the name of your constructor should match the name of your class.
Apex properties are like variables but have more capabilities and an increased level of control. With Apex properties, among other things, you can validate data before they change or trigger an action if that happens.
When creating an Apex property, you can include a get and a set accessor. The get code will run when a property is read and the set code when it gets a new value. A property could contain both accessors or only one of them. Here’s an Apex property example where the access modifier is public, and the return type is an integer:
Below, you can see a complete example of an Apex class with most of the components we discussed.
Salesforce’s Apex classes are one of the most powerful parts of the platform’s functionality. Classes let you define the properties and actions that your objects can perform.
Are you using Salesforce? Testim for Salesforce is a powerful AI-powered testing tool that will help you automate and simplify your Salesforce applications testing efforts.
Learn more about how Salesforce tests Salesforce and see how Testim can help.