Beginner 15 min read

Salesforce Record IDs: the Id Type

By the end of this lesson, you'll be able to:

  • Explain what an Id represents in Apex
  • Declare a variable of type Id
  • Distinguish the Id type from a plain String that happens to look like an ID

Prerequisites: "Date, Time, and Datetime"

Why Id gets its own type

Id accountId = '001XXXXXXXXXXXXXXX';
  1. Id accountId = '001XXXXXXXXXXXXXXX'; — declares accountId as an Id, not a String — Apex checks that the value is a validly-formatted Salesforce record ID as soon as it's assigned.

An Id looks like a String, but Apex treats it specially — if you assign a value that isn't a validly-formatted Salesforce ID, Apex throws an error immediately, rather than letting a malformed ID silently cause problems in a query later.

A real business example: Manufacturing

Id productionBatchId = someRecord.Id;
  1. Id productionBatchId = someRecord.Id; — reads the .Id field off an existing record (someRecord) and stores it — this is how you'd normally get a real Id value, rather than typing one by hand.

Any time you're holding onto "which specific record is this," an Id is the right type — whether it's a batch, an account, or a custom object record. You'll use this constantly starting in Module "DML: Changing Data Safely," where inserting a record gives you back its new Id.

Common mistakes

  • Using String instead of Id "because it\'s simpler." This throws away Salesforce's automatic format validation for no real benefit — prefer Id whenever a value genuinely represents a record identifier.

Exercise

Declare an Id variable called sampleId and assign it any validly-formatted 18-character Salesforce ID string, like '001000000000001AAA'. Debug it.

Show hint

Standard Salesforce IDs are 15 or 18 characters, starting with a 3-character object-type prefix (001 for Account, for example).

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Salesforce Record IDs: the Id Type Quiz

1. Why use Id instead of String for a record identifier?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 1/1

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Every Salesforce record has a unique identifier, and Apex has a dedicated Id type for it — distinct from String, even though an Id looks like text, because Salesforce validates its format automatically.