A concise, practical guide to Apex data types: primitives, sObjects, collections and best practices with usable examples for developers and admins.
What are Apex Data Types?
Apex data types define the shape and behaviour of the data your code works with — from primitive types like Integer and String to complex sObjects and collections (List, Set, Map). Choosing the right type improves accuracy, performance, and maintainability.
Primitive Types — Quick examples
Common primitive types you’ll use every day in Apex:
- Integer — whole numbers for counters and indices.
- Long — large integers and timestamps.
- Decimal — exact-precision numbers for currency.
- Double — floating-point for scientific math.
- String — text, immutable; use StringBuilder for heavy concatenation.
- Boolean — true/false flags in logic.
- Date / DateTime — date-only or timestamp values (DateTime stored in GMT).
- ID — Salesforce record identifier (15/18 chars).
- Blob — binary data for files and attachments.
Code snippets
// Integer and arithmetic Integer count = 100; Integer remainder = count % 7; // Decimal for money Decimal price = 199.99; Decimal tax = price * 0.0825; // DateTime DateTime now = DateTime.now(); Date tomorrow = Date.today().addDays(1); // ID usage Id accountId = '001D000001234567'; // Blob example Blob fileBlob = Blob.valueOf('Hello World');
sObjects & Field Access
sObjects are typed representations of records in Salesforce (Account, Contact, Custom_Object__c). You can access fields directly (acc.Name) or dynamically via put/get:
Account acc = new Account(); acc.Name = 'Acme'; acc.put('Industry', 'Technology'); String industry = (String) acc.get('Industry');
Collections: List, Set, Map
Collections let you handle multiple values efficiently. Use:
- List
when order and duplicates matter. - Set
for uniqueness and fast membership checks. - Map
for key-value lookups (very fast for Id->sObject maps from SOQL).
Listfruits = new List {'Apple','Banana'}; Set unique = new Set (fruits); Map acctMap = new Map ([SELECT Id, Name FROM Account LIMIT 10]);
Best practices & pitfalls
- Use Decimal for currency — never Double for money.
- Prefer the smallest numeric type that fits your range (Integer vs Long).
- Avoid string concatenation in loops — use StringBuilder.
- Pre-size collections when you know the expected size for minor performance gains.
- Always null-check IDs and collections before use.
- Be mindful of heap limits when using Blob and large collections.
When to use which type — quick decision guide
- Counting/indices: Integer
- Timestamps/large numbers: Long
- Financial math: Decimal
- Text: String (use StringBuilder for heavy ops)
- Record references/queries: ID
- Ordered groups: List; Unique groups: Set; Key lookups: Map
Conclusion
Understanding Apex data types is fundamental to writing correct, efficient, and maintainable Salesforce code. Use primitives for simple data, sObjects for record modeling, and collections to scale operations while following best practices to avoid common pitfalls like overflow, heap issues, and incorrect precision.
Why this matters: For admins, developers, and business users, choosing the right data type prevents bugs, reduces runtime errors, and helps meet performance and data-accuracy goals. Keeping types precise and operations efficient makes integrations, batch jobs, and UI logic more reliable.
Leave a Reply