# How do Auto-Number fields function in generating unique sequential identifiers for custom objects?

**Topic:** Admin · **Difficulty:** Medium · **Tags:** Admin, Auto-Number Fields, Data Types

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. Create a field of type Auto Number (or set the object's Name field type to Auto Number).
2. Enter a Display Format using `{0}` placeholders for digits, optional static text, and date tokens such as `{YYYY}`, `{YY}`, `{MM}`, `{DD}`.
3. Enter the Starting Number.
4. On each insert, Salesforce increments the counter and stores the formatted value.
5. 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:**
```text
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)
```

**Hidden gotchas & must-remember points:**
- Gaps are normal; never use auto-number for legal gapless sequences without extra design.
- Cannot be edited manually and cannot be reset retroactively; changing the start number does not alter existing records.
- Each field has its own counter; changing the data type of an auto-number field to another type is restricted.
- Sandbox copies carry the counter at the time of refresh, so numbers can collide between sandbox and production data loaded across orgs.
- Do not rely on it for uniqueness across orgs or after changing the format; use External ID separately if needed.
- Number digits within braces determine zero padding, and a number longer than the padding still expands without truncation.

**Best practice:** Use auto-number as a human-readable reference and keep the 18-character ID or an External ID as the system key.

---
Source: ForceChallenges · last updated 2026-11-02
