Sub topic 8
Required fields and username rules
This chapter assumes you can open Setup, you know what a profile and a user licence are from earlier chapters, and you have the Manage Users permission in a sandbox or developer org. The optional code sections use basic Apex and CSV.
What a user record needs
A user is a record on the User object. Salesforce will not save it until a small set of fields has values. Some of these fields look like personal details. Most of them control how the org behaves for that person: how dates display, which language the interface uses, and where notification emails go.
The required fields are:
| Field (API name) | Setup label | What it controls |
|---|---|---|
| `LastName` | Last Name | Display name. `FirstName` is optional. |
| `Alias` | Alias | Short name shown in list views and reports. Up to 8 characters. |
| `Email` | Where password and notification emails go. | |
| `Username` | Username | The login name. Must be unique across all Salesforce orgs. |
| `ProfileId` | Profile | Base permissions. Must be compatible with the licence. |
| `TimeZoneSidKey` | Time Zone | How date and time fields display for this user. |
| `LocaleSidKey` | Locale | Date, number and currency formats. |
| `EmailEncodingKey` | Email Encoding | Character set for outbound email, usually `UTF-8`. |
| `LanguageLocaleKey` | Language | Language of the interface and labels. |
In Setup, the Locale, Email Encoding and Language fields default to sensible values, so you usually only notice that they exist when you load data or write Apex. Both of those paths require you to set each value yourself.
The user licence has no field in this list because you never choose it directly. The profile you select determines it. That is why the profile is the field that most often causes a creation error.
Username rules
The username is the field that surprises people most often.
- It must be in the format of an email address, for example
[email protected]. It does not have to be a working mailbox. - It must be unique across every Salesforce org, not only your own. Another customer who already used
[email protected]blocks you from using it. - It is separate from the
Emailfield. Two people can share an email address, but each needs a different username. - Users log in with the username. Changing it later changes what they type at the login page, so tell the user.
Because uniqueness is global, a common admin pattern is to append a suffix that identifies the org. The examples in this tutorial use .demo:
- Production:
[email protected] - Sandbox copy:
[email protected]
Sandbox refreshes already do this for you. Salesforce appends the sandbox name to each copied username, so the copy of [email protected] in a sandbox called dev1 becomes [email protected]. This avoids a clash with the production user. If you create users by hand in a sandbox, you need to follow the same practice yourself.
Alias and nickname
The alias is limited to 8 characters and does not have to be unique, although a unique alias is easier to read in reports. A common scheme is the first letter of the first name followed by up to seven letters of the last name, for example alove. Experience shows that two people with similar names (such as two users named jsmith) will eventually collide on this scheme. That is allowed, but it makes list views harder to read.
Experience Cloud users also have a CommunityNickname field that must be unique. It does not apply to internal users, and this chapter does not cover it.
Time zone, locale and language
These three fields are easy to confuse.
- Time zone changes how stored date/time values are shown. The stored value does not change.
- Locale changes formatting only. A locale of
en_USshows12/31/2026. A locale ofen_GBshows31/12/2026. - Language changes the text of the interface, such as button labels and field labels.
A user can have a UK locale and a US English language. They are independent settings. When you create many users, set all three explicitly so you do not inherit the defaults of whoever ran the import.
How the fields map across the three creation methods
The fields are the same everywhere, but each method treats them differently.
| Method | Defaults filled in for you | Notes |
|---|---|---|
| Setup (single user) | Locale, language, encoding, time zone from the org defaults | Sends a welcome email by default |
| Add Multiple Users | Same defaults as Setup | Limited to the fields on the page |
| Data Loader or API | None | You must supply every required field |
| Apex | None | You must supply every required field |
Programmatic methods give you no help. If one field is missing, the insert fails with a REQUIRED_FIELD_MISSING error that names the field.
Loading users from a CSV
The Data Loader file needs one column per required field. Replace <ProfileId> with a real 15 or 18 character profile ID from your org.
LastName,FirstName,Email,Username,Alias,ProfileId,TimeZoneSidKey,LocaleSidKey,EmailEncodingKey,LanguageLocaleKey
Lovelace,Ada,[email protected],[email protected],alove,<ProfileId>,America/Los_Angeles,en_US,UTF-8,en_US
Hopper,Grace,[email protected],[email protected],ghopper,<ProfileId>,America/Los_Angeles,en_US,UTF-8,en_USTo find the profile ID, run this query in the Developer Console or in Data Loader's export wizard:
SELECT Id, Name, UserLicense.Name
FROM Profile
WHERE Name = 'Standard User'The query also returns the licence name. That confirms the profile belongs to a licence you have capacity for before you load the file.
Two things catch people here.
- Profile IDs differ between orgs. The ID for Standard User in your sandbox is not the ID in production. Do not reuse a CSV across orgs without replacing the column.
- Spreadsheet tools can damage the file. Excel may strip leading zeros or change the encoding. Save as CSV UTF-8 so names with accents survive.
Creating a user in Apex
The Apex version shows every required field set in one place. It is also a useful reference when you are unsure what a field expects.
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User u = new User(
FirstName = 'Ada',
LastName = 'Lovelace',
Alias = 'alove',
Email = '[email protected]',
Username = '[email protected]',
ProfileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert u;If the profile query returns no rows, the [... LIMIT 1] assignment throws a QueryException. In shared orgs, check the profile name before you run the snippet. Standard User exists in most orgs but may be renamed.
Checking a username before you create it
Because uniqueness is global, you cannot fully check availability from your own org. You can still catch the common case, which is a clash with a user in your own org, including inactive users. Deactivated users keep their usernames, so reusing a former employee's username fails.
SELECT Id, Username, IsActive
FROM User
WHERE Username = '[email protected]'If this returns a row with IsActive = false, you have two options. You can reactivate that record, or you can pick a different username. The old record cannot be deleted. If the query returns nothing and the save still fails with a duplicate error, the username belongs to another org.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| `DUPLICATE_USERNAME` | Username used in this or any other org | Change the username, for example by adding a suffix |
| `REQUIRED_FIELD_MISSING` | A required field is blank | Supply the field named in the message |
| `INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST` | A code such as a time zone is not valid | Use the API code, for example `America/Los_Angeles` |
| `INVALID_EMAIL_ADDRESS` | Email or username is not in email format | Correct the address |
| `LICENSE_LIMIT_EXCEEDED` | The licence behind the profile has no free seats | Free a licence or buy more, see the earlier chapter on capacity |
Read the message before changing anything. Salesforce names the field in most cases, and the fix is usually one value.
The creation flow at a glance
The order in the diagram is a way to think about debugging. The exact order inside Salesforce is not documented, so if you see one error, fix it and retry, because a second one may appear.
Exercise
You run a Data Loader job to insert 50 users. Every row fails with REQUIRED_FIELD_MISSING for LanguageLocaleKey, although the same users created one at a time in Setup worked fine. Why, and what do you change?
Show answer
Setup fills in Locale, Language, Email Encoding and Time Zone from the org defaults, but Data Loader does not. The CSV has no LanguageLocaleKey column, or the column is empty. Add the column with a valid code such as en_US on every row, and check that the other defaulted fields (TimeZoneSidKey, LocaleSidKey, EmailEncodingKey) are also present.
Exercise
A new hire's username [email protected] returns DUPLICATE_USERNAME. A SOQL query for that username in your org returns no rows. What does that tell you, and what should you do?
Show answer
Usernames are unique across all Salesforce orgs, so another org (or a deactivated record you cannot see through your filters) holds that name. Since your org has no match, the clash is elsewhere. Choose a different username, for example [email protected]. The email address can stay [email protected], because email and username are separate fields.