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
SFDC Admin & AnalystSFDC Admin & Analyst 

Duplicate Username DML Exceptions in Test Classes in Production

Test Classes in Production have failed suddenly with the following DML Exception. Please help.

 

Here is the 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]

 

Class.SGPRM_TestingClass.testSGPRM_OwnerChange_Before_Update: line 14, column 1

 

Here is the test class

 

@isTest
private class SGPRM_TestingClass
{

   //*#*Test Method for SGPRM_OwnerChange_Before_Update Trigger
    static testMethod void testSGPRM_OwnerChange_Before_Update() {
        
        Profile Profile1 = [select id from profile where name='System Administrator'];
        Profile Profile2 = [select id from profile where name='Standard User'];
        User CEO = new User(alias = 'ceo', email='admin@testorg.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = Profile1.Id,
        timezonesidkey='America/Los_Angeles', username='admin@testorg.com');
        insert CEO;
        User SalesManager = new User(alias = 'smngr', email='sManager@testorg.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = Profile2.Id, managerId = CEO.id,
        timezonesidkey='America/Los_Angeles', username='sManager@testorg.com');
        insert SalesManager;
        
        User SalesRep = [Select Id,Name,Email,managerId ,timezonesidkey from User where Name = 'Deborah BUELL' ];
        System.runAs(SalesRep)
        {
        Id RT = [select id from RecordType where SObjectType = 'Lead' AND developerName = 'DealRegistration'][0].Id;
        Lead lead = new Lead();
        lead.RecordTypeId = RT;
        lead.firstname = 'Test';
        lead.lastname = 'User';
        lead.Company = 'Test Group';
        lead.city = 'Test';
        lead.state = 'NJ';
        lead.country = 'United States';
        //lead.OwnerId = '005A0000000iviM';
        insert lead;

        Test.startTest();
        // Lead Submitted for Approval
        lead.Status = 'Submitted for Approval';
        lead.OwnerId = SalesManager.Id;
        update lead;
        Test.stopTest();
        }
    }}
Best Answer chosen by Admin (Salesforce Developers) 
SFDC Admin & AnalystSFDC Admin & Analyst

Hi Alex,

 

I figured it out what happened. Some other org created a username admin@testorg.com in prod and our org'd testmethods failed. So this was the problem.

 

My advice would be to choose a very unique name in test data and include your org name.

 

Thanks,

 

All Answers

Alex.AcostaAlex.Acosta

Do you have a user in your Prod org with the userName admin@testorg.com?

SFDC Admin & AnalystSFDC Admin & Analyst

Hi Alex,

 

I figured it out what happened. Some other org created a username admin@testorg.com in prod and our org'd testmethods failed. So this was the problem.

 

My advice would be to choose a very unique name in test data and include your org name.

 

Thanks,

 

This was selected as the best answer