function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fredkafredka 

Unable to deploy Code - "Duplicate_Username"

I just deployed code last week without a problem.  When I run all tests in the sandbox, I get 85% coverage and no errors.  However, when I try  to deploy anything from my sandbox, I get the following error on 3 of my test classes:

 

System.DMLException: Insert Failed. First Exception on row 0; first error; Duplicate_Username, Duplicate Username, <br> Another user has already selected the username <br> Please selectd another: [Username]

 

Each test class does create a user object and even though I have deployed these before without problem, I modified so they hae a differeint email address.... However I still cannot deploy.

 

I don't believe I have made any changes to the live org but I cannot deploy... any ideas? thanks!!!

 

Fred

Best Answer chosen by Admin (Salesforce Developers) 
Nathan CrosbyNathan Crosby

Hi Fred,

 

If you haven't already, I'd also check that the username you are creating in the test classes are unique and don't already exist in your destination org or any other org, not just the email address of the user's being created. Even though the test data isn't committed to the org, the duplicate validations will still fire on this field as it requires a unique username across all instances/orgs.

 

Regards,

Nathan

All Answers

Nathan CrosbyNathan Crosby

Hi Fred,

 

If you haven't already, I'd also check that the username you are creating in the test classes are unique and don't already exist in your destination org or any other org, not just the email address of the user's being created. Even though the test data isn't committed to the org, the duplicate validations will still fire on this field as it requires a unique username across all instances/orgs.

 

Regards,

Nathan

This was selected as the best answer
fredkafredka

Thanks for your help Nathan.. .that is what the problem was....  But what I am really confused about is that these test classes have been in place for a long time and never gave me a problem before...

 

In any event, that was exactly the problem... thanks for helping me!!!!! I appreciate it!!!!

 

Fred

Scott.MScott.M

I just ran into this, and I think that the key is that the username can't exist in ANY organization. So if you happen to pick a test username that somebody somewhere happend to pick for an actual user in your org then your test will fail. It just occured to me that it is possible that you select a username that no one has used yet in your test but as soon as someone uses it your tests will start to fail....  please tell me that isn't the case :) .... I guess we just need to make sure we pick really random ridiculous user names for our tests.

BruceVBurenBruceVBuren

Try this to create a random test user everytime.

 

    public static User createUser ( String userName, String lName, Id profileId ) {
        string randomName = string.valueof(Datetime.now()).replace('-','').replace(':','').replace(' ','');
        User newUser = new User(FirstName='Testttt'+lName,
                                 LastName=lName,
                                 UserName=randomName+userName,
                                 Email=randomName+userName, Alias=lName,
                                 LocaleSidKey='en_US', LanguageLocaleKey='en_US',
                                 EmailEncodingKey='ISO-8859-1', CommunityNickname=lName,
                                 ProfileId = profileId, TimeZoneSidKey='America/New_York');
        return newUser;
    } 

 

 

fredkafredka

Thanks Bruce!!!!

Jeff Linder - CRM ManagerJeff Linder - CRM Manager

Thanks for this info, helped me tag a very confusing bug.

 

I modified Bruce's code a little and thought it would be a good idea to share instead of testtt use the orgID.  That way if the code is deployed against multiple orgs, there's not even a miniscule chance of conflict...