Sub topic 9
Auto Number for system-generated identifiers
This chapter assumes you can create custom fields in Object Manager. It also assumes you know that every field has a type and an API name. It covers what an Auto Number field stores, how to set its display format, and where it breaks down as an identifier.
What an Auto Number field is
An Auto Number field holds a unique value that Salesforce generates when a record is created. Users cannot type into it, and they cannot edit it afterward. Salesforce increments a counter for each new record and formats the result with a pattern you define.
The stored value is text. A format such as INV-0001 is a string, so the field behaves like a read-only Text field, not like a Number field. Salesforce uses auto numbers for standard identifiers too. CaseNumber on Case and OrderNumber on Order are examples.
Use Auto Number when people need a short, readable reference for a record. Support agents can read out "00001042" on a call, which they could not do with a 15- or 18-character record ID.
The display format
The format has literal text plus placeholders in curly braces.
| Placeholder | Meaning |
|---|---|
| `{0}` | The counter, with no padding |
| `{0000}` | The counter, padded with zeros to four digits |
| `{YYYY}` or `{YY}` | Year of record creation |
| `{MM}` | Month of record creation |
| `{DD}` | Day of record creation |
A format of INV-{YYYY}-{0000} produces values like INV-2026-0001. The date parts come from the creation date. They do not update later.
You also set a starting number. Use it when you migrate from another system and want numbering to continue where the old one stopped. The whole formatted value is limited to 30 characters. Check the current limits in the Salesforce Help before you design a long format.
Padding and sorting
Because the value is text, sorting is alphabetical. Compare these values without padding:
INV-10INV-9
In alphabetical order, INV-10 comes before INV-9. Padding fixes this: {0000} gives INV-0009 and INV-0010, which sort correctly. Choose enough zeros for the volume you expect. If the counter outgrows the padding, it keeps counting. You get INV-10000, and that value sorts before INV-9999 as text.
SELECT Id, Invoice_Number__c
FROM Invoice__c
ORDER BY Invoice_Number__c DESC
LIMIT 10This query returns the latest invoices only if the padding is consistent. Often CreatedDate is the safer ordering field.
Defining one in metadata
In metadata format, the type is AutoNumber. The format goes in displayFormat and the first value in startingNumber.
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Invoice_Number__c</fullName>
<label>Invoice Number</label>
<type>AutoNumber</type>
<displayFormat>INV-{YYYY}-{0000}</displayFormat>
<startingNumber>1</startingNumber>
<externalId>false</externalId>
</CustomField>You can also use Auto Number as the record name of a custom object. In the object wizard, set the Data Type of the Name field to Auto Number. Then every record gets a readable name without anyone typing one.
Seeing it in Apex
Describe calls report the type as STRING. They also tell you the field is system-generated.
Schema.DescribeFieldResult f = Case.CaseNumber.getDescribe();
System.debug(f.getType()); // STRING
System.debug(f.isAutoNumber()); // true
System.debug(f.isCreateable()); // falseisCreateable() returns false. A DML insert cannot set the value, and an update cannot change it. This is also why a data load cannot import old invoice numbers into an Auto Number field. To keep legacy numbers, store them in a separate Text field marked as an external ID. This links to the indexing chapter, which covers external IDs.
Limits and gotchas
Gaps are normal
Numbers are not guaranteed to be consecutive. A record that is deleted keeps its number, and the number is never reused. A transaction that rolls back can also consume a number. Salesforce allocates numbers in batches under heavy load, so gaps can appear even when nothing is deleted.
If an auditor requires a gap-free sequence, Auto Number does not meet the requirement. You would need a custom approach, and that approach has its own concurrency problems.
The value is fixed at creation
Changing the display format or starting number affects how new records are numbered. Do not assume it renumbers existing records. Check the behavior in a sandbox first.
Converting from other types
Converting a Text field to Auto Number can assign numbers to existing records. Converting Auto Number back to Text keeps the current values and makes them editable. Both directions change who controls the data, so test them in a sandbox. The changing-field-types chapter covers the full list of consequences.
It does not replace the record ID
Auto Number values can look unique, but treat the Salesforce record ID as the real key. You can mark an Auto Number field as an external ID so other systems can look records up by it. Do not use it as a security token. A sequential value is easy to guess.
When to choose it
| Requirement | Good fit? |
|---|---|
| A readable reference number for tickets or invoices | Yes |
| A number with a year or month prefix | Yes, using date placeholders |
| A gap-free legal sequence | No |
| A value users must be able to correct | No, because it is read-only |
| Importing existing numbers from another system | No, use a Text field plus external ID |
Exercise
Your team wants invoice numbers like INV-2026-0001. A colleague proposes a Number field with precision 8 and scale 0, filled in by a flow. Give two reasons Auto Number is a better fit, and one requirement that would make Auto Number a poor fit.
Show answer
First, Auto Number generates the value at insert with no automation to maintain, and it is read-only, so users cannot overwrite it. Second, its display format supports the INV- prefix and {YYYY} placeholder directly, which a Number field cannot store because it holds only digits. A poor fit would be a legal requirement for a strictly gap-free sequence, because deleted or rolled-back records leave gaps in Auto Number values.
Exercise
In Apex, how can you confirm that a field is an Auto Number field, and what type does getType() return for it?
Show answer
Call isAutoNumber() on the field's DescribeFieldResult, for example Case.CaseNumber.getDescribe().isAutoNumber(), which returns true. getType() returns STRING, because the value is stored as text.