As per standard functionality, when one tries to create/update a user record with a username value that is NOT unique across all Salesforce orgs, then the following error message is returned:

Duplicate Username.
The username already exists in this or another Salesforce organization. Usernames must be unique across all Salesforce organizations. To resolve, use a different username (it doesn't need to match the user's email address).

I tried to bypass this with a before insert User trigger to say 'if any DML exception is hit, then append a value to the username' such that one wouldn't hit such errors.

I created the most granular debug log level but I don't see any DML exception or any other 'hook' that I could use for this.

It would seem that this exception is just not available to developers in any logs. Is this correct?

1 Answer 1


Correct, you cannot catch this error in a trigger. This error occurs before any triggers are executed. This is true for all standard validations, such as DUPLICATE_VALUE, REQUIRED_FIELD_MISSING, etc.

  • Thank you, sir. You are unbelievable! Nov 7, 2019 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.