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';

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;

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).

APEX

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.

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.