Sub topic 8
Required Fields and Username Rules
This chapter assumes you know what a user, a user licence and a profile are, and that you have the Manage Users permission in an org or sandbox. Here you will learn which fields a user record needs and how Salesforce checks the username. Most "cannot save" errors come from these rules.
The required fields
Every user record needs the following fields. The Setup form marks them as required. Data Loader and Apex do not mark them, so you must supply them yourself.
| Field | API name | What it does |
|---|---|---|
| Last Name | LastName | Display name. First Name is optional. |
| Alias | Alias | Short name of up to 8 characters, shown in some list views and reports. |
| Where Salesforce sends the login link and notifications. | ||
| Username | Username | The login name. It must look like an email address. |
| Profile | ProfileId | Sets the licence and the base access. |
| Time Zone | TimeZoneSidKey | Controls how dates and times are shown. |
| Locale | LocaleSidKey | Controls date, number and currency formats. |
| Email Encoding | EmailEncodingKey | Character set for outbound email, usually UTF-8. |
| Language | LanguageLocaleKey | Language of the Salesforce interface. |
In Setup, Salesforce fills Time Zone, Locale, Language and Email Encoding from defaults. Check them anyway, because a wrong time zone changes every date the user sees.
Some fields are required only in certain cases. A role is optional for most licences. A user whose profile uses a Salesforce licence does not need a manager. Some licence types need extra fields, such as a contact for a Customer Community user.
Username rules
The username has three rules:
- It must be in email format, such as
[email protected]. - It must be unique across all Salesforce orgs, not only your own. Two companies cannot hold the same username.
- It does not need to be a real mailbox. The Email field is the address that receives mail.
The second rule causes most failures. If someone already has [email protected] in any org, you cannot reuse it. Developer and sandbox orgs make this worse, because many people create the same test names.
A common pattern is to keep the real email in Email and add a suffix to the username. A sandbox copy of production gets a suffix like .sandbox appended to usernames automatically. You can use your own suffix, as in the sample data:
LastName,FirstName,Email,Username,Alias
Lovelace,Ada,[email protected],[email protected],aloveThe username can be up to 80 characters. Pick a naming scheme once and apply it everywhere, so that you can find users by pattern later.
Fields that are easy to confuse
- Username and Email are separate. Changing the email does not change the login name.
- Alias is not unique. Two users may share an alias. It is still required.
- Profile decides the licence. You cannot save the user with a profile whose licence has no free seats. The error mentions licences, not the profile.
Checking for a duplicate before you save
You can check whether the username exists in your own org with SOQL. This cannot see other orgs, so a clean result does not guarantee the name is free.
SELECT Id, Username, IsActive
FROM User
WHERE Username = '[email protected]'If the query returns a row with IsActive set to false, the name is held by a deactivated user. Usernames of deactivated users stay reserved, because users cannot be deleted.
Exercise
You try to create a user with the username [email protected]. Salesforce reports a duplicate, but a SOQL query on User in your org returns no rows. What is the most likely cause and the quickest fix?
Show answer
Usernames must be unique across all Salesforce orgs, so another org already holds that name. Keep the real address in Email and choose a different username, for example [email protected].