How do Auto-Number fields function in generating unique sequential identifiers for custom objects?
Answer
Basics: People need readable record identifiers: case numbers, invoice numbers, ticket numbers. Salesforce IDs are not human friendly. An auto-number field generates the value for you, read-only, so users and integrations cannot change it.
How it works:
- Create a field of type Auto Number (or set the object's Name field type to Auto Number).
- Enter a Display Format using
{0}placeholders for digits, optional static text, and date tokens such as{YYYY},{YY},{MM},{DD}. - Enter the Starting Number.
- On each insert, Salesforce increments the counter and stores the formatted value.
- Existing records receive numbers when you add the field to an existing object.
Advanced / deep dive: The counter is controlled by the platform, so you cannot write to the field from Apex or Data Loader (it is read-only on insert). Numbers are not guaranteed to be gap-free: rolled-back transactions, failed inserts, bulk inserts and lock contention can consume numbers without leaving a record. Changing the format or the starting number affects only future records and does not renumber existing ones. The display value is not a replacement for the record ID in relationships; use it as a human key and optionally mark the field as an External ID for upsert and search lookups. For truly gapless or per-scope sequences (a counter per account or per year), you need custom logic, for example a custom setting or an Apex sequence object with row locking, accepting concurrency cost.
Code / config example:
Field Type: Auto Number
Display Format: INV-{YYYY}-{0000}
Starting Number: 1
Sample value: INV-2026-0001
Formula use: TEXT(Invoice_Number__c) (auto-number is text-like and can be used in text formulas)