Data Types in Apex

Aman Garg
4 min readNov 14, 2023

What is a Data Type in Salesforce Apex?

In Salesforce Apex, a data type is like a category or a label that tells the computer what kind of information is stored in a variable (a storage box for information). It helps the computer understand how to handle and work with that information.

How Many Types of Data Are There in Salesforce Apex?

There are different types of data in Salesforce Apex, and they can be grouped into two main categories:

1. Data Types: These are the basic building blocks.

Below mentioned are the data types in Salesforce Apex:

  • Blob: Represents binary data.

Blob myBlob = Blob.valueOf(‘Hello, World!’);

  • Boolean: Represents true or false values.

Boolean isTrue = true;

  • Date: Represents a date without a time component.

Date myDate =;

  • DateTime: Represents a date and time.

Datetime myDatetime =;

  • Decimal: Represents a number with decimal places.

Decimal myDecimal = 42.2;

  • Double: Represents a 64-bit double-precision number.

Double myDouble = 42.2;

  • ID: Represents a record ID.


  • Integer: Represents whole numbers.

Integer myInt = 42;

  • Long: Represents large whole numbers.

: Long myLong = 4200000000;

  • String: Represents a sequence of characters.

String myString = ‘Hello, World!’;

  • Time: Represents a specific time of day.

Time MyTime = Time.newInstance(12, 30, 0, 0);

2. Collections in Salesforce: A collection in Salesforce Apex is like a basket where you can put many things together. It’s a special box that allows you to keep multiple pieces of information in one place. There are three main types:

• List: A list is like a shopping list where you can write down and keep many items in order.

Order Matters: In a list, the order of items is important. Each item has a specific position, called an index, and you can access items using their index.

Example — Adding and Accessing:

List<String> fruits = new List<String>{‘Apple’, ‘Orange’, ‘Banana’}; fruits.add(‘Grapes’);

// Adds ‘Grapes’ to the end of the list.

String myFruit = fruits[1]; // Accesses the second item (‘Orange’) using the index.

Use When Order Matters:

Use a list when the order of items is meaningful, like a sequence of steps or a timeline.

  • Set: A set is like a collection of unique treasures. It only keeps one of each kind.

No Duplicates:

Sets don’t allow duplicate values. If you try to add the same item twice, it only keeps one.

Example — Ensuring Uniqueness:

Set<Integer> uniqueNumbers = new Set<Integer>{1, 2, 3, 3, 4};

// The set will only have 1, 2, 3, and 4, eliminating the duplicate ‘3’.

Use When You Need Uniqueness: Use a set when you want to make sure each item is unique, like keeping track of distinct values.

  • Map: A map is like a dictionary where you match one thing with another. For example, you match a name with a phone number.

Key-Value Pairs:

Maps store information as key-value pairs. Each key is unique, and it maps to a specific value.

Example — Associating Information:

Map<String, Integer> ages = new Map<String, Integer>{‘Alice’ => 25, ‘Bob’ => 30, ‘Charlie’ => 22};

Integer bobAge = ages.get(‘Bob’); // Retrieves the age of ‘Bob’.

Use for Associations: Use a map when you want to associate one piece of information with another, like matching names with ages.

Collections help keep your information organized, like putting all your toys in one box or all your books on one shelf. They make it easier for your computer program to work with lots of data at once!

  • sObject Data Type: In Salesforce, a sObject is like a special container that holds data for a specific type of object, such as an Account, Contact, or Custom Object.

Example — Creating a sObject

Account myAccount = new Account(Name=’My Company’, Industry=’Technology’);

Here, myAccount is an instance of the Account sObject, holding information like the company name and industry.

  • Fields and Properties: A sObject has fields, which are like slots where you can store specific types of information (e.g., Name, Industry).

You access and modify these fields using dot notation.

  • Use for Database Operations: sObjects are often used when interacting with the Salesforce database. You can create, update, and query records using sObjects.


// Creating a new Account record and inserting it into the database.

Account newAccount = new Account(Name=’New Company’, Industry=’Finance’);

insert newAccount;

  • Custom Objects: sObjects aren’t just limited to standard objects like Account; they can also represent custom objects you create in Salesforce.


// Creating an instance of a custom object named ‘Custom_Object__c’.

Custom_Object__c customRecord = new

Custom_Object__c(Name=’Custom Record’);

In simple terms, think of a sObject as a special box that holds all the details about a specific type of thing in Salesforce, whether it’s a standard object provided by Salesforce or a custom object you’ve designed. It helps you work with and manipulate data related to that specific type of object.

  • Enum Data Type:

What is an Enum:

  • An Enum, short for Enumeration, is a special data type in Salesforce Apex that defines a set of named constant values.

Example -

Defining an Enum:

public enum DayOfWeek {



Here, DayOfWeek is an Enum that represents the days of the week as constant values.

Using Enum Values:

You can use Enum values to represent specific, predefined options in your code using Enum Values:

DayOfWeek today = DayOfWeek.WEDNESDAY;

Now, today holds the constant value representing Wednesday.


In simple terms, an Enum in Salesforce Apex is like a list of predefined options that you can use in your code. It helps make your code more readable and less error-prone by providing clear, named values for specific scenarios. Enums are especially useful when you want to represent a set of constant values that belong together.

Originally published at on November 14, 2023.



Aman Garg

Sr. Salesforce Developer || 5x Salesforce Certified || 2x Copado Certified || Salesforce Mentor || Founder of Salesforce Learners