Get Started with Apex
Learning Objectives
After completing this unit, you'll be able to:
- Define what code is.
- Describe where to store and edit code.
- Describe two ways to create code comments.
- Explain why code commenting is important.
So you're interested in extending your Salesforce knowledge to learn about Apex code? Well, you've come to the right place. Get ready to be guided step by step into the world of reading and writing code. It may be a little scary at first, but don't worry—we'll simplify even the most complex concepts.
As an admin, you already understand the ins and outs of Salesforce features and their current limitations. You have experience using Flow Builder to automate business processes and you're passionate about providing the best customized applications for your customers. As you go through this module, you'll be introduced to programming concepts first, followed by examples that you can test out on your own.
Ready to get started? Let's do it!
What Is Code?
Code is the language that humans use to talk to computers. We use code to write a precise set of instructions for performing a specific action. Think of code like a recipe. Each ingredient and instruction needs to be exact so that the dish turns out as you anticipated.
Just as people around the world use different languages, computers use different languages, such as JavaScript, Java, Ruby, Python, and many others. Although programming languages differ in many ways, they're all based on the same foundational structures and can all be interpreted by a computer to create applications. As you learn more about the Apex programming language, you'll start to understand programming structures.
Where Is Apex Code Stored?
Where do I start, you ask? First things first, we need to figure out where we store our code. Code is stored in files. These files can be local (on your PC or Mac), in the cloud (your Salesforce org), or they can be saved locally and synced automatically to the cloud. How cool is that?
The Developer Console
As an admin you've probably heard about something called the Developer Console. The Developer Console is a tool that developers use to create and edit files of code. Throughout this module, we use the Developer Console to store and execute (run) code in your Salesforce org. Let's take a look.
To access the Developer Console:
- First, make sure you are logged in to Trailhead.
- Then, click your user avatar in the upper-right corner of this page and select Hands-on Orgs from the dropdown.
- Click the username to launch your org. Want to create a new org? See the Trailhead Playground Management module to learn how.
- In your Trailhead Playground, click and select Developer Console.
Keep the Developer Console open. Throughout this module, we will use it to test code.
Writing Pseudocode
The most important part of any project you participate in as a Salesforce Admin is planning. Planning helps you to understand the needs of your customers, organize your own thoughts, and predict issues that you may encounter while developing the application.
During project planning, before developers begin writing Apex code, they write pseudocode. Pseudocode is a detailed but readable description of what a program or feature must do. It's written in natural language instead of a programming language. Pseudocode allows non-programmers to review the steps to confirm that the proposed code satisfies the requirements for the application. Developers write pseudocode in separate documents, on whiteboards, or in the Developer Console.
To get a feel for how writing pseudocode helps developers document their thinking about how to program an application, let's write the pseudocode to make a pot of tea for friends.
- In the Developer Console, choose File | New | Apex Class.
- Name the class
Teatime
. - Click OK.
- After you name the class, a window appears with a new class.
- Copy and paste these instructions above the existing class.
Get Utensils and Ingredients Boil Water in a Tea Kettle Make Tea in Teapot Add Tea and Sugar to a Teacup Serve Tea to Friend
Each pseudocode statement (instruction) should state an action. If you gave these instructions to a robot, do you think the robot could perform each step correctly?
Probably not. The robot would need to know which utensils and ingredients to get, how to boil water, and how much of each ingredient to add. This pseudocode needs more detail.
- Go back to the Developer Console.
- Replace the existing code with this code:
Get Utensils and Ingredients Get Teacup Get Spoon Get Tea Kettle Get Teapot Get Tea Get Sugar Boil water in a Tea Kettle Fill Kettle with Water Switch Kettle On Wait for Kettle to Boil Make Tea in Teapot Put Tea in Teapot Put Boiling Water in Teapot Wait 2 Minutes Add Tea and Sugar to Teacup Pour Tea in Teacup Put 1 teaspoon of Sugar in Teacup Stir Tea in Teacup Serve Tea to a Friend public class Teatime { }
When you write pseudocode, go through each step and break it down as much as possible to account for every scenario. For example, this pseudocode says to fill the kettle with water. What if the kettle is already full? Should the robot continue filling the kettle with water? Should it empty the kettle? When you think like a developer, you must think from different points of view and consider multiple possibilities.
Code Commenting
As an admin, have you ever seen a custom field that has no description? Unless that field has a very clear, descriptive name, its purpose may not be obvious. In code, comments describe the purpose of a block of code (one or more lines of code). Comments help developers understand what code to write, what existing code does, and what failing code is supposed to do. Developers embed comments within code, but enclose them in special characters that tell the computer not to read or run the comments.
Let's look at two ways to comment code: single line comments and multiline comments.
Single Line Comments
Single line comments begin with two slashes ( // ). The computer program ignores characters on the same line to the right of the // when it runs. Example:
Integer i = 1; //This comment will be ignored when the code is run.
Multiline Comments
Multiline comments begin with slash asterisk ( /* ) and end with asterisk slash ( */ ). Example:
Integer i = 1; /*This comment is ignored when the code runs*/
Let's turn the Teatime pseudocode into a comment. To keep the code development on track, it's a good practice to write your pseudocode like you would an outline.
- In the Developer Console, add the multiline comments to the pseudocode. It should look like this.
/*Get Utensils and Ingredients Get Teacup Get Spoon Get Tea Kettle Get Teapot Get Tea Get Sugar Boil water in a Tea Kettle Fill Kettle with Water Switch Kettle On Wait for Kettle to Boil Make Tea in Teapot Put Tea in Teapot Put Boiling Water in Teapot Wait 2 Minutes Add Tea and Sugar to Teacup Pour Tea in Teacup Put 1 teaspoon of Sugar in Teacup Stir Tea in Teacup Serve Tea to a Friend*/ public class Teatime { }
- Click File | Save.
In this module, you learn how to follow best practices when you write Apex code. This will set your foundation for becoming an extraordinary developer.
Now that you have your feet wet, let's dive in and start coding.