The CASESAFEID() function in Salesforce converts a 15-character record ID into its 18-character, case-insensitive version. You use it in formula fields when you need IDs that tools like Excel, VLOOKUP or an external database can match reliably, because those tools often treat “ABC” and “abc” as the same text.
In this tutorial, I will explain why Salesforce has two ID lengths, show the results I tested in a Salesforce Developer Edition org, and walk through creating an 18-character ID formula field step by step.
15-Character vs 18-Character IDs in Salesforce
Every Salesforce record has a unique ID, and Salesforce shows it in two forms (see 15 vs 18 digit Salesforce IDs for more background):
- 15-character ID: case-sensitive.
001Aand001aare different records. Salesforce shows this version in the user interface, in reports and in formulas. - 18-character ID: case-insensitive. It is the 15-character ID plus a 3-character suffix that encodes the upper and lower case of the first 15 characters. The API, Data Loader and record URLs use this version.
The problem appears when you export 15-character IDs to a tool that ignores case. Two different records can then look like duplicates. CASESAFEID solves this by giving you the 18-character ID inside Salesforce.
CASESAFEID Syntax
CASESAFEID(id)
- id: a record ID or a lookup field, for example
Id,AccountIdorOwnerId. - Returns: the 18-character ID as text, so the formula field’s return type must be Text.
CASESAFEID is meant for ID fields. Passing a normal text field, such as the Account Number, doesn’t make sense: the function only adds the case-safe suffix to a valid 15-character Salesforce ID.
CASESAFEID Results Tested in a Salesforce Org
I created a text formula field on the Contract object with this formula, then opened a contract record:
"Id = " & Id & " (" & TEXT(LEN(Id)) & " characters)" & BR() &
"CASESAFEID(Id) = " & CASESAFEID(Id) & " (" & TEXT(LEN(CASESAFEID(Id))) & " characters)" & BR() &
"Id = LEFT(CASESAFEID(Id), 15): " & IF(Id = LEFT(CASESAFEID(Id), 15), "TRUE", "FALSE")
| Expression | Result in the org |
|---|---|
Id | 800fj00002aeZcQ (15 characters) |
CASESAFEID(Id) | 800fj00002aeZcQAAU (18 characters) |
CASESAFEID(AccountId) | The related Account’s 18-character ID |
Id = LEFT(CASESAFEID(Id), 15) | TRUE |
Two things this shows: inside a formula, the Id merge field returns the 15-character version, and the 18-character result is simply the same 15 characters with three extra characters at the end. The 18-character value matched the ID in the record’s Lightning URL.
How to Create an 18-Character ID Formula Field
This is the most common use of CASESAFEID: a field you can add to reports and exports. The steps are the same on any object; this example uses Account.
- Go to Setup → Object Manager and open Account.
- Click Fields & Relationships, then New.
- Select Formula and click Next.
- Enter the Field Label Account ID 18 and choose Text as the return type. Click Next.
- Enter
CASESAFEID(Id)and click Check Syntax. You should see “No syntax errors in merge fields or functions.” - Click Next, choose which profiles can see the field, click Next, choose the page layouts and click Save.
Add the new field as a column in your reports and exports. For a report-focused walkthrough, see how to get the 18-digit Salesforce ID in a report, and for formula field basics, see how to create a formula field in Salesforce.
When to Use CASESAFEID
- Excel and Google Sheets lookups: VLOOKUP and MATCH are case-insensitive, so match on the 18-character ID to avoid false matches.
- Integrations and data migrations: external systems that store IDs in case-insensitive columns need the 18-character version.
- Comparing with API data: Data Loader and the API return 18-character IDs. A CASESAFEID field lets you compare report data with API exports directly. See how to export data from Salesforce.
- Links and keys in formulas: build URLs or matching keys with the full ID, for example with the HYPERLINK function.
Things to Keep in Mind
- The 15-character ID is case-sensitive and the 18-character ID is case-insensitive, not the other way round.
- Both versions point to the same record; Salesforce accepts either in URLs and the API.
- Use a Text return type. A CASESAFEID formula can’t be a Number or Date field.
- In Apex you don’t need CASESAFEID: an Id variable converted to a String already gives the 18-character version.