The Salesforce Definition of What the Apex Language is
Let’s go over what Apex actually is, and when you, as a developer or administrator, should use it when creating custom functionality in your Salesforce orgs.
Apex is a proprietary development language created specifically for Salesforce’s own platform. Apex looks and feels similar to Java (not Javascript) or C#. The Apex language allows you to build custom functionality that Salesforce’s point-and-click tools can’t support or do on their own. This means that with Apex, you can expand Salesforce’s functionality and build absolutely anything that you or your users can dream up for your Salesforce instance. The sky is the limit with the power of Apex! Go ahead and give it a try, and see what you can do with it.
A Simpler Explanation of What the Apex Language Is
Apex is a strongly typed object-oriented programming language, but what does that really mean? Let’s look at an example.
public class OpportunityCalculator { Integer numberValue = 1; String oppName = 'Cool Opp'; public Integer addOpportunityValues(Integer oppValueOne, Integer oppValueTwo){ Integer oppValuesCombined = oppValueOne + oppValueTwo; return oppValuesCombined; } public Integer substractOpportunityValues(Integer oppValueOne, Integer oppValueTwo){ Integer oppValuesCombined = oppValueOne - oppValueTwo; return oppValuesCombined; }
This example is a representation of a calculator for opportunities in our org. Like a real, physical calculator, this code calculator could potentially have the ability to add, subtract, multiply, and divide value. My opportunity calculator class above uses methods to add and subtract values, all while creating a representation of an object and the many ways in which that object can interface with its users (such as pressing a calculator’s buttons to do some math). There’s much more I could say about object-oriented programming, but that can should probably wait for a future blog post. It’s too much to unpack here, but eventually, we’ll check it out together!
There are four key things you need to know about object-oriented programming: encapsulation, abstraction, inheritance, and polymorphism. If you’d like to know more about those four in greater detail, check out my video over OOP in Apex here!
What is a Strongly Typed OOP Language?
public class OpportunityCalculator {
Integer numberValue = 1;
String oppName = 'Cool Opp';
public Integer addOpportunityValues(Integer oppValueOne, Integer oppValueTwo){
Integer oppValuesCombined = oppValueOne + oppValueTwo;
return oppValuesCombined;
}
public Integer substractOpportunityValues(Integer oppValueOne, Integer oppValueTwo){
Integer oppValuesCombined = oppValueOne - oppValueTwo;
return oppValuesCombined;
}
At a very high-level, object-oriented programming (OOP) is the representation of an object and its functionality in code. Now, what is a strongly typed language? First, consider variables, or a way to represent a value. When I create a variable called numberValue and I set it equal to 1, I’m declaring that variable as an integer, or a way to declare a number in Apex (among many other development languages). I’m declaring it as type Integer, but note that if I just declared that 1 as a number value without “Integer,” Apex wouldn’t accept it, nor would Apex allow me to save it like that. In short, Apex needs me to specify the type of value I’m trying to store: Integer. I must also specify what kind of value I want to return from those methods if I want to return something.
So, when I declare my method, I’m saying that when my method is done executing, it will return an integer. In programming languages that aren’t strongly typed, you don’t have to declare variables as a specific type (like Integer). Javascript is an example, where you can simply say that the variable you’re declaring equals 1 or Cool or whatever you can think up, and you don’t define a type for those variable.
In short, strongly typed OOP language means you’ve got to tell the code what it’s supposed to return and what your variable types are.
When Should You Actually Choose to Use Apex?
When exactly should you use the Apex development language? It’s a bit subjective, but also not. Often, you’ll find that out-of-the-box, point-and-click Salesforce stuff doesn’t work well with what your users actually need so they can do their job, and that’s when you use Apex. As a Salesforce user or admin, you must know all the administrative capabilities and know how to create new objects with their own fields, among other things. This is so you know when it’s proper to use administrative tools, or when you should use custom development instead.
Usually, you’ll need to use Apex development in one of two different scenarios. The first is, as mentioned earlier, when your out-of-the-box, point-and-click tools aren’t enough to build what you’re trying to make for your users. The second is when your org scales up to a huge size with lots of users (such as the 500-user mark), where your point-and-click tools can’t handle the sheer volume of daily transactions your users are making. When you reach that point, it’s time for Apex code to take over and work its magic!
Get Coding With The Force Merch!!
We now have a tee spring 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 Coding With The Force Merch 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
Apex Design Patterns Book
Good Non-SF Specific Development Books:
Clean Code
Clean Architecture
Design Patterns: Elements of Reusable Object-Oriented Software Book