Get Started With Apex

Salesforce Apex Slider with Segmented Progress Bar
Prev
Next

Lesson 1: Intro to Apex Variables

Imagine you have a cup. This cup can hold something inside it, like tea, juice, or water. The cup is like a container that keeps whatever you put inside it safe.

Now, in programming, we have something similar called a variable. A variable is like a container that holds information. Just like the cup holds tea, a variable holds data or information that we want to store and use later.

For example, let’s say you want to store the age of a student. You can create a variable (a container) and give it a name, like age. Then, you can put the number 20 inside it. So, it looks like this in Apex:

Integer age = 20;

Here, age is the variable (the container), and 20 is the information (the age of the student) stored inside it.

So, just like a cup holds tea, a variable holds information that we can use later in our program. Cool, right? 😊

Multiple Choice Questions

1. What is a variable in Salesforce Apex?
2. In Apex, how would you declare a variable to store the age 25?

Lesson 2: Data Types in Apex

In Salesforce Apex, data types define the kind of data a variable can hold. Choosing the right data type is important for efficient memory usage and correct operations.

Here are some common data types in Apex:

  • Integer: Used for whole numbers, e.g., Integer count = 10;
  • String: Used for text, e.g., String name = 'John';
  • Boolean: Used for true/false values, e.g., Boolean isActive = true;
  • Double: Used for decimal numbers, e.g., Double price = 19.99;

Each data type has specific use cases, and using the wrong type can lead to errors in your program.

Multiple Choice Questions

1. Which data type would you use to store the value "Salesforce"?
2. What data type is used for the value true or false?

Lesson 3: Working with Collections (Lists, Sets, Maps)

Collections in Apex are used to store multiple items in a single variable. Apex supports three main types of collections:

  • List: An ordered collection that allows duplicates, e.g., List names = new List(); names.add('John');
  • Set: An unordered collection with no duplicates, e.g., Set numbers = new Set(); numbers.add(1);
  • Map: A collection of key-value pairs, e.g., Map scores = new Map(); scores.put('John', 90);

Collections are powerful for managing and processing data efficiently in Apex.

Multiple Choice Questions

1. Which collection type allows duplicates?
2. How do you add a value to a List in Apex?

Skill Test: Apex Variable & Data Handling

Let’s test your understanding of Apex variables and data handling! In this skill test, you’ll apply what you’ve learned about variables, data types, and collections.

Here’s a quick recap:

  • Variables store data using specific data types (e.g., Integer, String).
  • Collections like Lists, Sets, and Maps help manage multiple values.
  • Always choose the right data type and collection for the task.

Answer the questions on the right to complete the test!

Multiple Choice Questions

1. What will happen if you try to store a decimal in an Integer variable?
2. Which collection would you use to store unique values?