Intermediate 15 min read

Record IDs: 15 vs 18 Characters

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

  • Explain the difference between the 15-character and 18-character forms of a Salesforce Id
  • Recognize why Apex always uses the 18-character form for comparisons
  • Understand what makes the 18th-character suffix unique to the 18-character form

Prerequisites: "Relationships in Apex"

Two formats, same record

15-character: 001XX000003DHP0
18-character: 001XX000003DHP0YAO

Both refer to the exact same Account record. The 15-character form is what a user sees in the browser's URL bar; the 18-character form is what the API (and Apex) always uses internally.

Why the 15-character form is risky

The 15-character Id is case-sensitive001XX000003DHP0 and 001xx000003dhp0 would be treated as different Ids, even though a careless copy-paste or a case-insensitive system (like a spreadsheet or some databases) could easily mix up the casing. This makes it a poor choice for programmatic comparison.

The extra 3 characters solve this

Id recordId = '001XX000003DHP0YAO'; // 18 characters

The 18-character form appends 3 extra characters that encode the case of the original 15 characters — making the full 18-character Id case-insensitive for comparison purposes. This is exactly why Apex's Id type and every SOQL query always deal in the 18-character form: it removes an entire category of "these look equal but technically aren't" bugs.

Exercise

As a comment, explain why comparing two 15-character Ids with == could produce a wrong result even when they refer to the same record.

Show hint

Think about case sensitivity.

APEX

Record IDs: 15 vs 18 Characters Quiz

1. Why does Apex always use the 18-character Id form internally?

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 two valid Id formats — a 15-character case-sensitive one and an 18-character case-insensitive one — and Apex always works with the 18-character form to avoid a subtle class of bugs.