Apex: A Crash Course
Salesforce, like many major platforms with individual purposes, employs a native programming language uniquely tailored it its needs. Apex—Salesforce’s language—is similar to Java, while also possessing a number of custom features specifically for the Salesforce environment. The Apex Developer Guide calls Apex a "strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Lightning platform server in conjunction with calls to the Lightning Platform API.” While that description may seem intimidating, it’s not as scary as it seems—let’s take a quick tour of Apex fundamentals.
Apex is strongly typed: values generally have immutable types. An integer, for example, must always be treated as an integer, and a string must be treated as a string. In a strongly typed language, variable behavior isn’t hidden; it’s explicit what variables are doing and how variables are being used. This practice makes strongly typed languages easier to understand than weakly typed ones where variable behavior may be unpredictable. In a weakly typed language such as C, for example, a string may be used as an integer, which confuses the programmer.
Apex is also object-oriented. As opposed to function-oriented programming, where data and data behaviors are kept separate, object-oriented programming encapsulates data and behaviors in structures called objects. Objects (not to be confused with the native Salesforce concept of sObjects) can be used any number of times to accomplish the programmer’s purposes, often through classes and methods. A
Book object, for example, might include methods for assigning authors, page counts, and titles to extant books while also creating new ones.
Additionally, Apex uses flow and transaction control statements comparably to Java and similar programming languages. Developers can use
else statements and
while loops to conditionally execute code blocks as many times as needed. If a programmer wanted to create one hundred sObjects, for example, she can write a
while loop to create all of them at once, as opposed to separately declaring all of the sObjects line by line.
Unlike Java, however, Apex is uniquely integrated with the Salesforce platform and hosted on Salesforce’s servers. When developers use Apex to write Lighting Web components, they can work directly with the data hosted in their orgs and manipulate it as needed through database queries and searches. Apex also provides custom methods for working with Salesforce sObjects and other ideas specific to the platform.
Apex is tailored for Salesforce, so a better understanding of one increases understanding of the other. Proficiency comes with practice, so while complex phrases like “strongly typed” and “flow control statement” may seem intimidating, being able to use an
if statement correctly without remembering the technical term for it is fine. The internet is full of resources for learning Apex and other languages—we’ve barely scratched the surface—but whether a Salesforce veteran or just taking first steps on the trail, a working knowledge of Apex is fundamental to using Salesforce well.
Interested in hearing more about how the CATLab is using Apex? Follow the CATLab on social media here: