Sub topic 9
Auto Number for system-generated identifiers
This chapter assumes you know what objects, records and fields are, and that you can create a custom field in Object Manager. It covers the Auto Number field type: what it generates, how to format it, and where it goes wrong.
What Auto Number does
An Auto Number field is a read-only text field that Salesforce fills in when a record is created. Users can't type into it. Each new record gets the next value in a sequence, such as INV-0001, INV-0002 and so on.
Use it when people need a short, readable identifier. The 18-character record Id is unique, but nobody wants to read it over the phone. Standard objects already use this idea. Cases have a CaseNumber, and Contracts have a ContractNumber.
Setting the display format
You define the format with two parts.
- Display Format is the pattern. Literal text is written as is, and each
{0}placeholder stands for one digit of the counter. For example,INV-{0000}producesINV-0001. - Starting Number is the first value of the counter. If you set it to 1000 with
INV-{0000}, the first record getsINV-1000.
You can use up to 10 placeholder digits. The format can also contain date tokens such as {YYYY} or {MM}. A format like {YYYY}-{0000} gives 2026-0001. Note that the date token is only part of the displayed text, and it does not restart the counter each year.
<?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-{0000}</displayFormat>
<startingNumber>1</startingNumber>
</CustomField>Behavior to know before you choose it
The counter is not a gap-free count. Treat the values as unique labels, not as a record tally.
- Gaps are normal. If a record is rolled back after a number was assigned, or a bulk insert fails, that number is not reused. Deleting a record also leaves a hole.
- Existing records. If you add the field to an object that already has records, Salesforce numbers the existing ones. The order isn't guaranteed to match creation date.
- Values are text. The stored value is a string, so
INV-10sorts beforeINV-9if you don't pad with zeros. Pad the format ({0000}) to keep sorting predictable. - Overflow. If the counter passes the digits in your format, the number simply gets longer, so
INV-{000}becomesINV-1000after 999. - Unique and external ID. You can mark an Auto Number as unique, and as an external ID, which makes it useful for integrations that need to look up a record by its number.
- Changing the format. Editing the display format changes how new records look. Existing values keep the old format.
Auto Number compared with a formula
Don't use a formula field to fake an identifier from other fields. A formula recalculates, so it can change when its inputs change. An Auto Number is assigned once and does not change.
Exercise
A team sets the display format to ORD-{000} and starts at 1. After the 1,000th record is created, what does the number look like, and why might sorting the field as text cause trouble?
Show answer
The 1,000th record gets ORD-1000, because the number grows past the three placeholder digits instead of failing. As text, ORD-1000 sorts before ORD-200 because the characters are compared left to right. Use enough placeholder digits up front, such as ORD-{000000}, to keep sorting consistent.