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
Jeff_SFJeff_SF 

why is this error happening when triggers are tested in production?

The same error is not happening in sandbox.  I am atempting to add a trigger and update another.  this same erro is happening on several failed tests.  I am not able to deploy for this reason.

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]
Best Answer chosen by Jeff_SF
Maharajan CMaharajan C
HI,

Just change ur username in test class it will solve the problem

The Username field is unique across all orgs. Try using a value less likely to collide. Something like testUser+asdfghjkl@example.com.1234ABCD. You can randomize it more by adding static counters, Math.random, etc. Here are a couple options for your one-off case.
'testUser+asdfghjkl@example.com.' + Math.random()
'testUser+asdfghjkl@example.com.' + Datetime.now().getTime()

Please refer the below things from salesforce:

How to resolve DUPLICATE_USERNAME error returned while performing a deployment
Knowledge Article Number000182266
DescriptionI am performing a deployment using a change set and am getting the below error:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>Another user has already selected this username.<br>Please select another.: [Username]"Failure Stack Trace: "Class.DuplicationxxTest.DeDuplication: line x, column 1" 

How can I fix this issue?
ResolutionUsernames are shared across instances, but not across environments (production/sandbox). I.e. if a user account in a sandbox instance has username = a@b.c, this username cannot be used in any other sandbox instance (CS*), but it can be used in production instances (NA*, EU*, AP*)

Uniqueness is enforced during deployments when tests are run, so an insert call will fail if the username is already registered in another org in the same environment (production or sandbox). The resolution for this is to use a guaranteed unique username for tests. We recommend generating globally unique usernames, e.g. org Id + timestamp + random value, as the below code snippet shows:
 
public static User createTestUser(Id roleId, Id profID, String fName, String lName) {
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
    User tuser = new User(  firstname = fName,
                            lastName = lName,
                            email = uniqueName + '@test' + orgId + '.org',
                            Username = uniqueName + '@test' + orgId + '.org',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias = uniqueName.substring(18, 23),
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId = profId,
                            UserRoleId = roleId);
    return tuser;
}


Thanks

All Answers

Jeff_SFJeff_SF
PS All usernames were checked and there are no duplicates
Maharajan CMaharajan C
HI,

Just change ur username in test class it will solve the problem

The Username field is unique across all orgs. Try using a value less likely to collide. Something like testUser+asdfghjkl@example.com.1234ABCD. You can randomize it more by adding static counters, Math.random, etc. Here are a couple options for your one-off case.
'testUser+asdfghjkl@example.com.' + Math.random()
'testUser+asdfghjkl@example.com.' + Datetime.now().getTime()

Please refer the below things from salesforce:

How to resolve DUPLICATE_USERNAME error returned while performing a deployment
Knowledge Article Number000182266
DescriptionI am performing a deployment using a change set and am getting the below error:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>Another user has already selected this username.<br>Please select another.: [Username]"Failure Stack Trace: "Class.DuplicationxxTest.DeDuplication: line x, column 1" 

How can I fix this issue?
ResolutionUsernames are shared across instances, but not across environments (production/sandbox). I.e. if a user account in a sandbox instance has username = a@b.c, this username cannot be used in any other sandbox instance (CS*), but it can be used in production instances (NA*, EU*, AP*)

Uniqueness is enforced during deployments when tests are run, so an insert call will fail if the username is already registered in another org in the same environment (production or sandbox). The resolution for this is to use a guaranteed unique username for tests. We recommend generating globally unique usernames, e.g. org Id + timestamp + random value, as the below code snippet shows:
 
public static User createTestUser(Id roleId, Id profID, String fName, String lName) {
    String orgId = UserInfo.getOrganizationId();
    String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
    Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
    String uniqueName = orgId + dateString + randomInt;
    User tuser = new User(  firstname = fName,
                            lastName = lName,
                            email = uniqueName + '@test' + orgId + '.org',
                            Username = uniqueName + '@test' + orgId + '.org',
                            EmailEncodingKey = 'ISO-8859-1',
                            Alias = uniqueName.substring(18, 23),
                            TimeZoneSidKey = 'America/Los_Angeles',
                            LocaleSidKey = 'en_US',
                            LanguageLocaleKey = 'en_US',
                            ProfileId = profId,
                            UserRoleId = roleId);
    return tuser;
}


Thanks
This was selected as the best answer
Jeff_SFJeff_SF
thank you