Experience Salesforce
Apex Data Types
What You’ll Learn
- What Are Apex Data Types?
Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
We can define a variable as ‘named value holder.’ The variable can hold a value on the basis of its data type. The data type determines what data could be stored inside a variable such as a number, text, date, etc. Data type also defines what is the storage limit of the space taken by the variable. Apex data types in Salesforce are similar in that they categorize the information. Let’s take a look at what are different data types in Salesforce Apex and how to check a data type in Apex.
What Are Apex Data Types?
Apex Datatypes is a data type that can be assigned to a variable. By defining the data type of a variable, you tell the programming language what kind of information that variable can represent and how it can be manipulated within your code. This helps prevent errors and ensures your program works as intended. Apex is a strongly typed language, i.e., we must declare the datatype of a variable when we first refer to it. All apex variables are initialized to null by default.
The data types in Apex can be broadly categorized into
- Primitive
- sObject
- Collection
- Enum.
1. Primitive Data Types
Primitive data types in Apex are the fundamental building blocks for storing basic data values. They represent specific types of information and define the operations that can be performed on that data. All Apex variables, whether they’re class member variables or method variables, are initialized to null. Make sure that you initialize your variables to appropriate values before using them. For example, initialize a Boolean variable to false.
Primitive Data Types can be divided into following categories:
- Boolean: Represents actual or false values.
- Integer: Represents a 32-bit number without a decimal point.
- Long: Represents a 64-bit number without a decimal point.
- Decimal: Represents a 64-bit number with a decimal point. It’s used for precise calculations, like currency.
- Double: Represents a 64-bit number with a decimal point. It’s similar to Decimal but has double precision.
- Date: Represents a date value consisting of a day, month, and year.
- DateTime: Represents a specific date and time, including time zone.
- Time: Represents a particular time without a date.
- String: Represents a sequence of characters, like words or sentences.
- ID: Represents a Salesforce record’s unique identifier.
- Blob: Represents binary data used for storing files or images.
2. sObject Data Types
sObject data types represent any object that can be stored in the Salesforce platform database. This includes standard objects like Account, Contact, Lead, Opportunity, and custom objects.
For Example
Account myAccount = new Account(Name='Acme Corporation', Industry='Technology');
// This creates an sObject variable named `myAccount` that can hold data for a single Account record.
Read More:
sObjects in Salesforce
3. Collection Data Types
The collection data type can store more than one value.
- List (or Array): An ordered collection of elements that can contain duplicates.
- Set: An unordered collection of elements that does not contain duplicates.
- Map: A collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, and values can be any type.
Read More:
Collections in APEX
4. Enum (Enumerated) Data Types
Represents a set of named constants, restricting a variable to a specific set of predefined options. Enums are used when a variable can only take one out of a small set of possible values.
For Example
public enum OrderStatus {
New,
Processing,
Shipped,
Completed
}
Order myOrder = new Order();
myOrder.setStatus(OrderStatus.New);
Next Topic
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.