Id
Learning Content
Last updated: August 03, 2026
Id Data Type
The Id data type is a Salesforce-specific primitive data type used to uniquely identify every record stored in the Salesforce platform. Whether it is an Account, Contact, Opportunity, Case, Lead, or any custom object, each record is assigned a unique Id when it is created.
Although a Salesforce record Id looks like text, it should be stored using the Id data type instead of a String. The Id data type provides type safety and allows Salesforce to validate record identifiers automatically.
Every Salesforce developer works with record Ids daily while writing Apex, SOQL queries, Triggers, Flows, Lightning Web Components, and integrations.
💡 LearnFrenzy Insight
Think of an Aadhaar number, passport number, or employee ID.
Many people may have the same name, but their identification number is always unique.
Similarly, Salesforce uses a unique Id to identify every record in the database.
Two Accounts can have the same company name, but they will never share the same Salesforce Id.
What is an Id?
An Id uniquely identifies a Salesforce record. It is automatically generated by Salesforce when a new record is inserted into the database.
Account acc = new Account();
acc.Name = 'LearnFrenzy';
insert acc;
Id accountId = acc.Id;
System.debug(accountId);
Salesforce Record Id Formats
Salesforce commonly uses two formats for record Ids.
| Type | Length | Description |
|---|---|---|
| Case-Sensitive Id | 15 Characters | Mainly used within the Salesforce user interface. |
| Case-Insensitive Id | 18 Characters | Commonly used in APIs, integrations, and external systems. |
Variable Declaration
Id accountId = '0015g00000ABCDEFGA'; System.debug(accountId);
The value above is an example only. Every Salesforce record receives its own unique Id.
Obtaining a Record Id
Account accountRecord =
[
SELECT Id, Name
FROM Account
LIMIT 1
];
Id recordId = accountRecord.Id;
System.debug(recordId);
Using Id in SOQL
Id accountId = '0015g00000ABCDEFGA';
Account accountRecord =
[
SELECT Name
FROM Account
WHERE Id = :accountId
];
System.debug(accountRecord.Name);
Real Salesforce Business Example
🏢 Business Scenario A customer support application receives an Account Id from a Lightning Web Component and retrieves the Account details.
public static Account getAccount(Id accountId)
{
return
[
SELECT Id, Name, Industry
FROM Account
WHERE Id = :accountId
];
}
Using the Id data type clearly indicates that the parameter must contain a valid Salesforce record identifier.
Common Salesforce Use Cases
| Business Requirement | Example |
|---|---|
| Retrieve Account | Account Id |
| Update Opportunity | Opportunity Id |
| Delete Contact | Contact Id |
| Create Relationship | Parent Record Id |
| REST API Integration | Record Identifier |
| Lightning Navigation | Record Page Id |
Id vs String
| Id | String |
|---|---|
| Stores Salesforce record identifiers. | Stores general text. |
| Automatically validated by Salesforce. | No record validation. |
| Type-safe for database operations. | General-purpose text type. |
| Best choice for record references. | Best choice for names, emails, and descriptions. |
15-Character vs 18-Character Id
| 15-Character Id | 18-Character Id |
|---|---|
| Case-sensitive | Case-insensitive |
| Mostly seen in the Salesforce UI | Commonly used by APIs and integrations |
| Less suitable for external systems | Preferred when exchanging data externally |
How Salesforce Generates Record Ids
Insert Record
│
▼
Salesforce Generates
Unique Record Id
│
▼
Stores Record
│
▼
Id Returned to Apex
│
▼
Available for Queries & Updates
🔍 Behind the Scenes
New Account Created
│
▼
Unique Id Generated
│
▼
Database Record Stored
│
▼
Id Assigned to Object
│
▼
Available Throughout Transaction
✅ Best Practices
- Use the
Iddata type instead ofStringwhen storing Salesforce record identifiers. - Use descriptive names such as
accountId,contactId, oropportunityId. - Pass Id values directly into SOQL queries using bind variables.
- Use the 18-character Id for integrations and external systems.
⚠️ Common Mistakes
- Using
Stringinstead ofIdfor record identifiers. - Hardcoding record Ids in production code.
- Confusing 15-character and 18-character Id formats.
- Assuming two records can share the same Id.
🎯 Interview Tip
Question: Why should you use the Id data type instead of String for Salesforce record identifiers?
Answer: The Id data type is specifically designed for Salesforce record identifiers. It provides type safety, allows Salesforce to validate the identifier automatically, and makes Apex code clearer and easier to maintain than using a general-purpose String.
📌 Quick Revision
- ✔ Every Salesforce record has a unique Id.
- ✔ Use the
Iddata type instead ofStringfor record identifiers. - ✔ Salesforce supports both 15-character and 18-character Id formats.
- ✔ The 18-character format is preferred for integrations.
- ✔ Record Ids are commonly used in SOQL, DML, Apex, Flows, LWC, and integrations.
- ✔ Salesforce automatically generates a unique Id when a record is inserted.
➡ Next Lesson
In the next lesson, you'll explore the Blob data type, learn how Apex stores binary data such as files and images, and understand common use cases involving file processing, attachments, and content management.
Practice What You've Learned
Test your understanding with these practice exercises