Short answer
Use the built-in CASESAFEID() function in Salesforce formulas to convert a 15-character (case-sensitive) ID to its 18-character (case-insensitive) equivalent. In validation rules, use CASESAFEID() for comparisons or REGEX/LEN to validate 18-character IDs.
Why 15 vs 18 characters?
Salesforce stores record IDs in two formats: a 15-character case-sensitive ID (used in the Salesforce UI) and an 18-character case-insensitive ID (used by the API and external systems). The extra 3 characters are a checksum that makes the ID case-insensitive.
Formula examples
1) Formula field (Text) that returns the 18-character ID of the current record:
CASESAFEID(Id)
2) Convert a custom text field that contains a 15-character ID (e.g., Custom_Id__c
) to 18 characters:
CASESAFEID(Custom_Id__c)
3) Compare a user-entered ID (which may be 15 or 18 chars) to the record Id in a formula (safe, case-insensitive):
CASESAFEID(Id) = CASESAFEID(Custom_Id__c)
Validation rule examples
If you want to enforce that a text field stores the 18-character version, use one of these approaches.
a) Block saving unless the field is exactly 18 alphanumeric characters:
NOT(REGEX(Custom_Id__c, "^[A-Za-z0-9]{18}$"))
b) Block saving if the field looks like a 15-character ID (force users to provide the 18-char value):
AND(NOT(ISBLANK(Custom_Id__c)), LEN(Custom_Id__c) = 15)
c) Compare ignoring case — convert both sides to 18-char and then compare (useful when validating against another Id):
CASESAFEID(Id) <> CASESAFEID(Custom_Id__c)
When to use CASESAFEID()
– In formula fields to display or normalize IDs to the 18-character form.
– In validation rules when comparing Ids or ensuring case-insensitive equality.
– In reports, integrations and lookups where external systems expect 18-character IDs.
Notes & tips
- CASESAFEID() returns an 18-character string. It is the simplest and most reliable method in formulas to normalize IDs.
- For integrations, always prefer 18-character IDs to avoid case-sensitivity issues with external systems (Windows file systems, Excel, etc.).
- If a custom field may contain non-ID values, add defensive checks (ISBLANK, LEN, REGEX) before using CASESAFEID() to avoid unexpected results.
If you need a formula-only implementation of the 15->18 conversion algorithm (checksum-based) without CASESAFEID(), that is possible but very long and error-prone — prefer CASESAFEID() unless you have a specific restriction.
Leave a Reply