Apex Data Types in Salesforce: A Comprehensive Guide
Introduction
Apex is a strongly-typed programming language used in Salesforce. It requires variables to be declared with a specific data type before usage. Data types define the type of data a variable can hold and the amount of memory required for storage.
Salesforce provides several types of data, including:
- Primitive Data Types
- sObject Data Types
- Collection Data Types
- Enumeration Data Types
- Primitive Data Types
Primitive data types are fundamental data types that allow developers to work with simple values in Apex. These include:
Integer
- Stores whole numbers (without decimal points).
- Range: -2^31 to 2^31 – 1.
- Each Integer variable occupies 4 bytes of memory.
- Used in Number, Currency, and Percent fields.
Example:
Integer num1 = 20;
System.debug(‘The Value of num1: ‘ + num1);
Long
- Stores larger whole numbers.
- Range: -2^63 to 2^63 – 1.
- Each Long variable occupies 8 bytes of memory.
Example:
Long num3 = 1234567890123456L;
System.debug(‘Long Value: ‘ + num3);
Double
- Stores floating-point numbers with decimal places.
- Range: -1.79E308 to 1.79E308.
Example:
Double pi = 3.1415926535;
System.debug(‘Value of Pi: ‘ + pi);
Decimal
- Stores precise decimal values, mainly used for Currency fields.
- Occupies 8 bytes of memory.
- Example:
Decimal price = 199.99;
System.debug(‘Price: ‘ + price);
String
- Stores alphanumeric values and special characters.
- Used for Text, Text Area, Email, Phone, URL, Picklist, etc.
- Example:
String name = ‘Salesforce Apex’;
System.debug(‘Course Name: ‘ + name);
Date
- Stores only date values (YYYY-MM-DD format).
- Occupies 8 bytes of memory.
- Date.today() method retrieves the current date.
Example:
Date todayDate = Date.today();
System.debug(‘Today’s Date: ‘ + todayDate);
DateTime
- Stores both date and time.
- Used in DateTime fields.
- Occupies 8 bytes of memory.
- Example:
DateTime now = System.now();
System.debug(‘Current DateTime: ‘ + now);
Boolean
- Stores true or false values.
- Used in Checkbox fields.
Example:
Boolean isActive = true;
System.debug(‘Is Active? ‘ + isActive);
ID
- Stores 18-character Salesforce record IDs.
- Used in Lookup and Master-Detail relationships.
Example:
ID accountId = ‘0015g00000abcD1F’;
System.debug(‘Account ID: ‘ + accountId);
- sObject Data Type
- Represents Salesforce standard or custom objects.
- Used to store records from Salesforce objects.
Example:
Account acc = new Account(Name = ‘ABC Corp’);
System.debug(‘Account Name: ‘ + acc.Name);
- 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.
Example:
List<String> names = new List<String>{‘Alice’, ‘Bob’, ‘Charlie’};
System.debug(‘Names List: ‘ + names);
Set (Unique Collection)
- Stores unique values.
Example:
Set<String> uniqueNames = new Set<String>{‘Alice’, ‘Bob’, ‘Charlie’};
System.debug(‘Unique Names: ‘ + uniqueNames);
Map (Key-Value Pair Collection)
- Stores values in a key-value pair.
Example:
Map<Integer, String> studentMap = new Map<Integer, String>{1 => ‘Alice’, 2 => ‘Bob’};
System.debug(‘Student Map: ‘ + studentMap);
- Enumeration (Enum) Data Type
- Represents a fixed set of constants.
- Example:
public enum Status { Active, Inactive, Pending }
Status currentStatus = Status.Active;
System.debug(‘Current Status: ‘ + currentStatus);
Conclusion
Understanding data types in Apex is crucial for efficient Salesforce development. Primitive types handle simple values, sObjects manage Salesforce records, Collections store multiple items, and Enums define fixed constants. Mastering these data types enables developers to write efficient, scalable Apex code.