Experience Salesforce

Apex Data Types

What You’ll Learn

  • What Are Apex Data Types?

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 

  1. Primitive
  2. sObject
  3. Collection
  4.  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.

  1. List (or Array): An ordered collection of elements that can contain duplicates.
  2. Set: An unordered collection of elements that does not contain duplicates.
  3. 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);
salesforce-developer
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.

cts-img
Rakshabandhan Sale