Sub topic 15
Fixing common creation errors
This chapter covers the errors you are most likely to meet when you create Salesforce users in Setup, with Data Loader, or with Apex. It assumes you can already create a single user, know the difference between a user licence, a profile and a permission set, and can read a basic SOQL query. For each error you will see the cause, how to confirm it, and the fix.
A method for diagnosing errors
Most user creation failures fall into a few groups. Read the error message first, because Salesforce usually names the field or limit involved. Then work through the checks below in order.
- Is there a licence available for the profile you chose?
- Is the username unique and in the correct format?
- Are all required fields filled in with valid values?
- Is the profile compatible with the licence, and does it exist in this org?
- Does a permission set you assigned need a permission set licence that has no free seats?
The flow below shows this order.
Exhausted licences
The most common error in Setup is a message saying the licence limit has been exceeded, or a profile that does not appear in the Profile list. A profile is tied to one user licence. When every seat of that licence is in use, you cannot create another active user on any profile that uses it.
Check capacity with this query in the Developer Console or Workbench:
SELECT Name, TotalLicenses, UsedLicenses
FROM UserLicense
ORDER BY NameIf UsedLicenses equals TotalLicenses for the licence you need, you have three options.
- Deactivate users who no longer need access. This releases their seats.
- Ask your Salesforce account executive to add licences.
- Choose a profile that uses a different licence, if that licence still fits the person's work.
Remember that a frozen user still holds a seat. Freezing only blocks login. Only deactivation releases the licence.
Duplicate usernames
A username must be unique across all Salesforce orgs, not only your own. It must also look like an email address, for example [email protected]. It does not have to be a working mailbox. The username is separate from the Email field, which is where notifications and the welcome email go.
A typical error message is "Duplicate Username" or "Username already exists in this or another Salesforce organization". The cause is usually one of these:
- The person already has a user in another org, such as a developer edition or a trial.
- A previous user with that username was deactivated. Deactivated users keep their usernames, because users cannot be deleted.
- A copy and paste mistake in a bulk file repeated the same username on two rows.
The fix is to change the username. A common convention is to append the environment name, for example .dev or .demo, which also makes sandbox and production usernames easy to tell apart. When you copy a production org to a sandbox, Salesforce appends the sandbox name to each username automatically.
If the clash is with a deactivated user in your own org, open that user and rename the old username. Do not reactivate the old user just to free the name.
Required fields and invalid values
Every user needs these fields: LastName, Alias, Email, Username, ProfileId, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey and LanguageLocaleKey. In Setup the form fills in some of these for you. In Apex and Data Loader you must supply all of them.
| Message or symptom | Likely cause | Fix |
|---|---|---|
| Required fields are missing: [Alias] | Alias left blank in a file or Apex | Provide an alias of up to 8 characters |
| Alias is too long | More than 8 characters | Shorten the alias |
| Invalid email address | Typo or missing domain | Correct the `Email` value |
| bad value for restricted picklist field: America/Los Angeles | Wrong format for a time zone, locale or language value | Use the API value, such as `America/Los_Angeles` |
| Invalid cross reference id | `ProfileId` is wrong, or a placeholder was left in | Query the real profile ID and paste it in |
| Profile ID not found | The ID is from another org | Look up the ID in the target org |
The last two rows deserve attention in bulk loads. Profile IDs differ between production and sandboxes, so a CSV prepared in one org will fail in another. Look the ID up in each org:
SELECT Id, Name, UserLicense.Name
FROM Profile
WHERE Name = 'Standard User'The UserLicense.Name column also confirms which licence the profile consumes, which helps with the capacity check above.
Profile and licence mismatches
A profile belongs to exactly one user licence type. You cannot pair a user with a profile from a different licence family than the one you meant to use. Two typical symptoms:
- The profile you expect is missing from the drop-down in Setup. The licence it uses may be exhausted, or you may be creating a user from a screen that filters by licence, such as Add Multiple Users for a given licence type.
- A Data Loader row fails because the profile uses a licence that has no free seats.
Query the profile as shown above, then compare its licence against your UserLicense results. If the licence is correct but access is too narrow, do not switch to a more powerful profile. Keep the minimal profile and add a permission set. This keeps your access design aligned with least privilege.
Permission set assignment errors
Assigning a permission set can fail even when the user was created without trouble. The usual message is that the permission set requires a permission set licence, or that the user's licence is not compatible with the permission set. A permission set can be tied to a user licence, to a permission set licence, or to neither.
Check permission set licence capacity first:
SELECT MasterLabel, TotalLicenses, UsedLicenses
FROM PermissionSetLicense
ORDER BY MasterLabelIf the permission set needs a permission set licence, the user must hold that licence before the assignment will succeed. In Setup you can assign the licence from the user's detail page, under Permission Set License Assignments. In Apex you insert a PermissionSetLicenseAssign record first, then the PermissionSetAssignment.
Other assignment errors you may see:
- Duplicate assignment. The user already holds the permission set directly, or through a permission set group. Query
PermissionSetAssignmentfor the user before inserting. - Permission set not found. The API name in your script is a placeholder or belongs to another org.
- Assignment to an inactive user. Reactivate the user first, or skip them in your script.
Errors specific to bulk loads and Apex
With Data Loader, check the error file that the tool writes after each run. It repeats each failed row and adds an ERROR column. Fix the rows in that file and load only those, so you do not create duplicates.
An Apex insert of several users can fail partway. With a plain insert, one bad row rolls back the whole statement. To keep the good rows, use Database.insert with allOrNone set to false and inspect each result:
List<User> newUsers = new List<User>();
// ... build the list of User records here ...
Database.SaveResult[] results = Database.insert(newUsers, false);
for (Integer i = 0; i < results.size(); i++) {
if (!results[i].isSuccess()) {
for (Database.Error err : results[i].getErrors()) {
System.debug('Row ' + i + ' ('
+ newUsers[i].Username + '): '
+ err.getStatusCode() + ' - ' + err.getMessage());
}
}
}Two further gotchas apply to Apex.
- User is a setup object. Inserting a user in the same transaction as a non-setup object such as an Account causes a mixed DML error. Move the user work into a separate transaction, for example a
@futuremethod or a Queueable class. - Users created by Apex or Data Loader do not always trigger a welcome email. Check the
EmailEncodingKeyand email settings, and use the password reset option in Setup if the person receives nothing.
Problems after the user exists
Some issues appear only when the person tries to log in.
- No welcome email. Check spam filters and confirm the
Emailvalue. Email deliverability in a sandbox is often set to system email only, which stops messages to users. Change it under Setup, Deliverability. - User can log in but sees nothing. The profile grants no access to the objects they need, or a permission set was never assigned.
- User cannot see records. This is a record visibility issue, so check the role, sharing settings and ownership instead of the profile.
- User is locked out. Use the Unlock button on the user record, or reset the password.
Quick reference
| Error group | First check | Typical fix |
|---|---|---|
| Licence limit | `UserLicense` query | Deactivate users or add licences |
| Duplicate username | Search the username in your org | Change the username |
| Missing or invalid field | Compare with required field list | Use API values for picklists |
| Profile problem | `Profile` query with licence name | Use the correct org's profile ID |
| Permission set error | `PermissionSetLicense` query | Assign the licence first |
| Mixed DML | Apex transaction design | Separate user and non-setup DML |
Exercise
A Data Loader run to create 50 users fails for 12 rows. The error file shows "Invalid cross reference id" on the ProfileId column for all 12, while the other 38 rows loaded. The CSV was prepared from a production export. What is the most likely cause, and what do you do next?
Show answer
The 12 failing rows most likely contain a profile ID from production, while you are loading into a sandbox or another org, where profile IDs differ. Query the Profile object in the target org for the correct IDs, update the ProfileId column in the error file only, and reload that file. Do not reload the original file, or the 38 users that succeeded would fail as duplicates.
Exercise
You create a user successfully, but inserting a PermissionSetAssignment fails with a message that a permission set licence is required. Which query do you run first, and what are the two possible outcomes?
Show answer
Run a query on PermissionSetLicense for the relevant licence and compare TotalLicenses with UsedLicenses. If a seat is free, insert a PermissionSetLicenseAssign for the user, then repeat the PermissionSetAssignment insert. If no seat is free, free one by removing the licence from a user who no longer needs it, or buy more, before you assign.