Sub topic 16
Unique and external ID fields, and changing a field type
This chapter assumes you can create custom fields in Object Manager and run basic SOQL. It covers two things. The first is how unique and external ID settings change the way records are matched and queried. The second is how to change a field's type without losing data.
Unique and external ID fields
A unique field rejects any value that already exists on another record of the same object. You choose whether "ABC" and "abc" count as duplicates (case-insensitive) or as different values (case-sensitive).
An external ID field holds the key from another system, such as an ERP customer number. It is available on Text, Number, Email and Auto Number fields. An object can have up to 25 external ID fields.
The two settings are independent. A field can be unique, an external ID, or both. If the outside system's key is truly unique, enable both.
Why they affect queries
Salesforce automatically indexes unique and external ID fields. An index lets the query engine find matching rows without scanning the whole object. Filtering on an indexed field is fast on large objects, as long as the filter is selective, meaning it matches a small share of records. A filter like ERP_Id__c = 'A-1001' is highly selective. A filter on a field where most records share one value is not.
External IDs also drive upsert, which updates a record if the key exists and inserts it if not. You don't need to look up the Salesforce Id first.
Account acct = new Account(Name = 'Acme', ERP_Id__c = 'A-1001');
Database.UpsertResult result = Database.upsert(acct, Account.ERP_Id__c, false);
System.debug(result.isCreated() ? 'Inserted' : 'Updated');
List<Account> found = [SELECT Id, Name FROM Account WHERE ERP_Id__c = 'A-1001'];This example assumes ERP_Id__c is a Text external ID field on Account.
Changing a field type safely
Salesforce lets you change many field types after creation, but the conversion rules differ by pair. Existing values are converted, not preserved as they were. Two checks come before any change:
- Where is the field used? Formulas, validation rules, flows, reports and Apex that depend on the old type can break or return different results.
- What happens to existing values? Check each value against the new type's rules.
| Change | Typical risk |
|---|---|
| Number to Text | Low. Digits become characters. |
| Text to Number | Values that aren't numeric are lost. |
| Text to Picklist | Distinct values become picklist entries. Large numbers of distinct values are a problem. |
| Multi-select picklist to single-select | Records with several selections can lose data. |
| Reducing length, precision or scale | Values can be truncated or rounded. |
When a change is risky, create a new field with the right type, copy the data over, update the dependencies, then retire the old field. This keeps the original data until you have verified the result.
Exercise
You need to upsert Accounts from an ERP system by its customer number, which is always unique. Which field settings should you use, and what must you do first if the field already exists with duplicate values?
Show answer
Make the field a Text, Number, Email or Auto Number field with both Unique and External ID enabled. First remove or fix the duplicate values, because enabling Unique fails while duplicates exist.