0% found this document useful (0 votes)
6 views10 pages

Apex Data Types in Salesforce

The document provides an overview of Apex data types in Salesforce, detailing their definitions, memory requirements, and usage. It categorizes data types into Primitive, sObject, Collection, and Enumeration types, explaining each with examples. Best practices for using these data types effectively in Apex programming are also highlighted.

Uploaded by

SANTHOSH D K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Apex Data Types in Salesforce

The document provides an overview of Apex data types in Salesforce, detailing their definitions, memory requirements, and usage. It categorizes data types into Primitive, sObject, Collection, and Enumeration types, explaining each with examples. Best practices for using these data types effectively in Apex programming are also highlighted.

Uploaded by

SANTHOSH D K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Apex Data Types in Salesforce

Kajal Yadav

Apex Data Types 1


 Apex Basic Development Topics

Data Types:

Apex is a Tightly coupled programming language, which requires the variable


declaration before it's usage. Upon defining the variable, we have to specify the
datatype.

Datatype describes, 2 things as below.

1. What type of data, the variable can hold.


2. How much memory is required to store the value inside the variable.
o Types of Data in Salesforce
• Primitive Data Type
• Sobject Data Type
• Collection Data Type
• E-Numeration Data Type

Primitive Data Type:

Apex Provide the Primitive Data Type to allow the Developer to work with simple values
in your code. Developers can use this data type to declare variables, perform Operation
and Manipulation data. Traditional Data types available across the programming
language .

There are Different Primitive data types in Apex:

1. Integer
2. Long
3. Double
4. Decimal
5. String
6. Date
7. Date and Time
8. ID
9. Boolean
10. Blob (Binary Large Objects)

Integer:

This datatype will store the Numerical values, which doesn't have decimal points inside it.

Note: It will store the values between the Range: -2^31 to +2^31 - 1. Each integer
variable will occupy 4 bytes of memory.

Note: Each Number field, Currency field, and percent field will reference the "Integer"
Datatype by default.

Apex Data Types 2


Use to store Whole Number
Syntax :
Data Type<Variable Name>=<Value> Limit in between -2,147,483,648
to 2,147,483,648

Ex:

Integer x=10; Integer y=20;


Integer num1=20;
system.debug('The Value is num1=> '+num1);

Output:

Long:

This Datatype is used to store the Larger values. Which allows us to store

the values between the Range : -2^63 to +2^63 - 1. Each Long value will

occupy 8-bytes of memory.

Syntax :

Data Type<Variable Name>=<Value> Long num3 =


1234567890123456L;
More than 9 Digits
Limites : -9.223,372,036,775,808 to 9.223,372,036,775,808

Apex Data Types 3


Example

Double:

Used to store Floating point numbers with decimal Places


Limits of double data type is -1.79E308 to 1.79E308
Example:

Decimal:

This datatype is used to store Decimal values. Which occupies 8-bytes of memory to
store each value.

Note: Currency field reference the Decimal datatype by default.

Apex Data Types 4


Example:

String:

This Datatype allows us to store alphanumerical values along with the special characters.
Note: Each Text field, Text Area, Text Area Long, Text Area Rich, Email,
Phone, URL, Text Encrypted, Picklist, Picklist-Multiselect, etc. fields will
reference the "string" datatype by default.
Syntax:
String <VariableName>=<Variable Value>;

Example:

Apex Data Types 5


Date:

This datatype is used to store "Date" values. Each Date field inside the object
will reference the Date Datatype by default.

Note: Each Date value will occupy 8- bytes of memory

Syntax: Date <Variable Name>=<Variable Value>;


Date.Today() method is used to get today ‘s date

Example:

DateTime:

This Datatype is used to store DateTime stamp values. Which should be


referenced by "DateTime" field types.

Note: Each DateTime value will occupy 8-bytes of memory.

For Example refer the above example of Date type

ID:

This data type is used to store the 18-characters id value generated by salesforce.
Note: All ID fields, Lookup, Master-Details field types will reference the "ID datatype"
by default.

Apex Data Types 6


Boolean:

This datatype will store the TRUE / FALSE values inside it.

Note: Each Checkbox field will reference the "Boolean" datatype by default.

Blob (Binary Large Objects):

This Datatype is used to store Images / Pictures / Audio Files / Videos/


Attachment/ etc.

All the Attachments, Images, pictures will convert into Binary format and the
Binary format content will be storing inside the "Blob Datatype". Each Blob
Datatype variable can store the value of the size max. 2 GB

 sObject Data Type

 Represents Salesforce standard or custom objects.

 Used to store records from Salesforce objects.

Note:

1. All sObject datatypes will supports Dynamic Memory allocation. (i.e.


memory will be allocated dynamically at runtime of the application.)

2. sObject datatype values will get resides in the "Heap Memory".

Apex Data Types 7


Example:

 Collection Data Types

Collections store multiple values within a single variable. Apex supports three types:

List (Ordered Collection)

 Stores multiple values in an ordered manner.

 List Collection allows us to store both Homogeneous and Heterogeneous elements.

Example:

Set (Unique Collection)

 Stores unique values.

 Set collection supports the Dynamic memory allocation, Hence the memory size can
Grow / Shrink at runtime.

Apex Data Types 8


Example:

Map (Key-Value Pair Collection)

 Stores values in a key-value pair.

 Key and Value can be either Primitive / SObject / Apex / Collection / User Defined

type.

Example:

 Enumeration (Enum) Data Type

 Represents a fixed set of constants.

Apex Data Types 9


Example:

 Best Practices for Data Types in Apex

✅ Use Specific Types → Prefer Integer, Decimal, Boolean over Object for better type
safety.
✅ Avoid Using String for Numbers → Use Integer or Decimal instead of String for
numeric values.
✅ Use Collections Wisely →

 List for ordered data

 Set for unique values

 Map for key-value pairs

✅ Always Initialize Collections → Prevent NullPointerException.


✅ Use sObject for Queries → Instead of Object, use List<Account> for Account
records.
✅ Prefer Boolean for Flags → Avoid Integer or String for true/false conditions.
✅ Use Blob for Large Data → Efficient storage for files & attachments.
✅ Declare Constants → Use final for fixed values to avoid hardcoding.
✅ Use Enum for Fixed Value Sets → Ensures type safety instead of using Strings.

Apex Data Types 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy