Sub topic 15
Fixing common creation errors
This chapter assumes you can create a user in Setup, know what a profile and a permission set are, and can run a basic SOQL query. It covers the errors you are most likely to see when you create users one at a time or in bulk, and how to diagnose each one.
How to read a creation error
A user insert can fail at several points. Salesforce checks the required fields, then the username, then the licence and profile pairing, then the available licence count. The message names the field or rule that failed. Read it before you change anything.
The same errors appear in Setup, in Data Loader error files, and in Apex DmlException messages. Only the format differs.
Duplicate username
The message is usually Duplicate username or Username already exists. A username must be unique across all Salesforce orgs, not only your own. It must also look like an email address, though it does not need to be a working one.
To fix it, add a suffix that identifies the org or environment, as the examples in this tutorial do with .demo. Do not reuse the email address as the username if that person already has a user in another org.
Licence limit exceeded
The message is LICENSE_LIMIT_EXCEEDED in the API and Apex. It means every licence of the type tied to the profile is in use. Check capacity first.
SELECT Name, TotalLicenses, UsedLicenses
FROM UserLicense
ORDER BY NameIf UsedLicenses equals TotalLicenses for the licence you need, you have two options. You can deactivate a user who no longer needs access, which releases the licence. Or you can ask your Salesforce account team to add licences. Freezing a user does not release a licence.
The same error can occur with permission set licences. Run the equivalent query on PermissionSetLicense before you assign a permission set that depends on one.
Profile and licence mismatch
Every profile belongs to one user licence. A new user takes the licence of the profile you pick. If the profile you choose is not available, the usual cause is that the org has no licence of that type.
In bulk loads, the cause is often a wrong ProfileId. A profile ID from a sandbox is not valid in production, because IDs differ between orgs. Query the profile in the target org and use that ID.
Missing or invalid required fields
The errors look like REQUIRED_FIELD_MISSING or INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST. Check these first:
LastName,Email,Username,AliasandProfileIdare all present.TimeZoneSidKey,LocaleSidKey,EmailEncodingKeyandLanguageLocaleKeyhold valid API values, such asAmerica/Los_Angelesanden_US, and not display labels.Aliasis no longer than eight characters.
Common errors at a glance
| Error text | Likely cause | Fix |
|---|---|---|
| Duplicate username | Username exists in any org | Use a unique username |
| LICENSE_LIMIT_EXCEEDED | No free licences | Deactivate a user or add licences |
| REQUIRED_FIELD_MISSING | Blank required field | Complete the field |
| INVALID_CROSS_REFERENCE_KEY | Wrong or foreign `ProfileId` | Query the ID in this org |
Fixing a bulk load
In Data Loader, open the error CSV after a run. It repeats each failed row with an ERROR column. Fix the rows in that file and load it again. Do not reload the original file, or the rows that succeeded will fail as duplicates.
Exercise
A Data Loader run creates 40 of 50 users. The error file shows LICENSELIMITEXCEEDED for the other 10. A colleague suggests freezing ten inactive users to make room. Will this work, and what should you do?
Show answer
No. Freezing blocks login but the user still holds a licence. Deactivate ten users who no longer need access, or ask for more licences. Confirm with a query on UserLicense that UsedLicenses is below TotalLicenses, then reload only the error file.