How to handle DUPLICATE_USERNAME error while performing deployment
Recently, while doing deployment through change set, I faced an issue -System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.Another user has already selected this username.Please select another.: [Username]”The username used in test classes were definitely unique, but still my test classes were failing with the below error message. After spending some quite amount of time, I have understood the problem. So thought of putting this in my blog hoping that it may help someone in future.So here it goes -Error message -System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.Another user has already selected this username.Please select another.: [Username]”Reason -Usernames are always shared across instances, but not across environments. It means if a user is having username as test@test.com in any sandbox, then this same username you can’t use in any other sandbox instances (CS*), but you can user the same username in production sandboxes (NA*, EU*, AP*). Uniqueness is always checked during deployment when tests are run. So an insert from test class will also fail if the username is already registered in another org in the same environments. So we need to make sure the uniqueness in maintained in test classes.Resolution -To be 100% sure that the username used in test classes are unique, you should follow the below approach to define the username – String orgId =...
Read More