I am trying to validate a change set and it's giving me a Duplicate Username error. The problem is that the same test passes in 2 other sandboxes. From my understanding if the same username is being used for the test method in a different sandbox instance the error should be present there as well. However this is not the case. Here's the error.

System.DmlException: Insert failed. First exception on row 0; first error: 
DUPLICATE_USERNAME, Duplicate Username.<br>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). : [Username] 

Any ideas why this is happening?

EDIT I changed the username and pushed a new change set. The test passed. The sandbox I am pushing to is FULL and the sandbox I am pushing from is a Partial Copy sandbox.

  • Are you using SeeAllData?
    – EricSSH
    Feb 13 '18 at 21:26
  • No I am not using it.
    – Arthlete
    Feb 13 '18 at 21:27
  • How many times has it failed this validation? If your method for creating test User records doesn't inject enough randomness, you may have a non-negligible chance of a naming collision. If it only failed once, run it again.
    – Adrian Larson
    Feb 13 '18 at 21:30
  • I also thought about the username not being random enough but if that was the case it would fail in the other 2 sandboxes too. I ran the test 3 times and it failed 3 times for the same error. Then I went back to the other sandbox and ran it there - no problem. I just tried again and it fails again. What I did was to change the username and push a new change set - test passed. Still a mystery why it passes in the other 2 sandboxes.
    – Arthlete
    Feb 13 '18 at 21:33
  • Maybe the same username is being used by some other test classes which is not present in other two Sandboxes. Are you sure all three Sandboxes are in sync in terms of test classes?
    – Saroj Bera
    Feb 13 '18 at 21:45

If this only errors in the 1 box it's most likely that the user actually exists out side of the test class in that specific org. Test this by printing the user to the debug log with system.debug and then querying to see if it exists.

Regardless of the reason, the issue is self explanatory. Here is some sample logic I've used in the past.

public static User newUser() {
    Id profileId = [
        SELECT Id
        FROM Profile
        WHERE Name = 'System Administrator'

    String uniqueEmail = String.valueOf(Math.round(Math.random()*1000)) +
        string.valueOf(System.now().format('ddMMYYYhhmmss')) +
        '@' + URL.getSalesforceBaseUrl().getHost();

    User result = new User(
        FirstName = 'Grumple',
        LastName = 'Frillskin',
        Alias = 'gfrilly',
        Email = uniqueEmail,
        UserName = uniqueEmail,
        ProfileId = profileId

    return result;


  • Avoid generic terms such as "Test" or "Sample"
  • Use the org url so that the chance of the user existing outside this org is minimal
  • Use random number + time to make the username as unique as possible
  • FirstName, LastName, Email, and Alias do not need to be unique\
  • Using the host url does run the risk of duplicates in the event of a refresh (as you will not be on the same host and someone else on your new host may be using similar logic) consider adding the org ID as well.
  • Thanks for your response. I already had checked and there's no such username in the sandbox.
    – Arthlete
    Feb 14 '18 at 0:15
  • Username is globally unique, across all production instances and separately across all sandbox instances.
    – Adrian Larson
    Feb 14 '18 at 15:24
  • @AdrianLarson I'm not sure how what I said contradicts what you're saying.
    – gNerb
    Feb 14 '18 at 15:41
  • "If this only errors in the 1 box it's most likely that the user actually exists out side of the test class in that specific org." That assertion is incorrect. This error doesn't indicate anything about the org in which the test is run. That's what's strange about it failing in one org and not others.
    – Adrian Larson
    Feb 14 '18 at 15:42
  • Unless the method creating a user uses logic similar to mine in that it uses some identification from the instance in the generated user name. So the same test run in two different instance generates two distinct usernames such as "test@cs9.salesforce.com" and "test@cs8.salesforce.com". In that instance, it could fail in just 1 sandbox
    – gNerb
    Feb 14 '18 at 15:45

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.