+ Start a Discussion
stefandlstefandl 

Deployment Results: error on Apex Test

Hello,

 

I've created a trigger and corresponding class test in sandbox.

Test in SB works perfectly, but during deployment in production an error occurred.

 

Trigger is:

 

 

trigger NewCaseFromOpp on Opportunity (after update) {

//Query for Case record types
     List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Case' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> CampaignsRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
     CampaignsRecordTypes.put(rt.Name,rt.Id);
     
     
//Query for Users 

    List<User> users = [Select Name, Id From User];
    
//Create a map between the User Name and Id for easy retrieval

     Map<String,String> CaseUsers = new Map<String,String>{};
     for(User  usr: Users)
     CaseUsers.put(usr.Name,usr.Id);

 
 for (Opportunity opp : Trigger.new) 
 { 
    if ( (opp.approval_status__c == 'Approved by AD' ) &&
         (trigger.oldMap.get(opp.id).approval_status__c != 'Approved by AD' ) && 
         (opp.Implementation_DirectTrack__c == 'Yes') )
    { 
       Case c = new Case ( 
       Opportunity__c = opp.Id, 
       Status = 'New', 
       Origin = 'Web', 
       Type = 'Internal Case', 
       Reason = 'Campaign  Setup', 
       Subject = 'New Direct Track Implementation', 
       RecordTypeId=CampaignsRecordTypes.get('Traffic'),
       OwnerId = CaseUSers.get('Traffic List') ); 
       insert c; 
    } 
    else if ((opp.approval_status__c == 'Approved by AD' ) &&
         (trigger.oldMap.get(opp.id).approval_status__c != 'Approved by AD' ) && 
         (opp.Implementation_DirectTrack__c == 'No'))
    { 
       Case c = new Case ( 
       Opportunity__c = opp.Id, 
       Status = 'New', 
       Origin = 'Web', 
       Type = 'Internal Case', 
       Reason = 'Campaign  Setup', 
       Subject = 'New Affiliation Campaign', 
       RecordTypeId=CampaignsRecordTypes.get('Traffic'),
       OwnerId = CaseUSers.get('Traffic List')  ); 
       insert c; 
    } 
    else
    {
    } 
 } 
}

 

 

Apex Test:

 

 

/**
 * This class tests the trigger named AddCampaign.
 */
@isTest
private class CaseFromOpp{

    static testMethod void TriggersTest() {       
                    
//Data Prep
        
        User usr = [Select Id,Name,ProfileId,TimeZoneSidKey,LocaleSidKey,EmailEncodingKey,LanguageLocaleKey  from User limit 1];
        User user1 = new User ();
        user1.LastName = 'Tester';
        user1.ProfileId = usr.ProfileId;
        user1.TimeZoneSidKey = usr.TimeZoneSidKey;
        user1.LocaleSidKey = usr.LocaleSidKey;
        user1.EmailEncodingKey = usr.EmailEncodingKey;
        user1.LanguageLocaleKey = usr.LanguageLocaleKey;
        user1.alias = 'tst';
        user1.Email = 'tester@test.com';
        user1.Username = 'tester@test.com';  
        insert user1;
        
//Create Account, Opportunity, Product, etc.
        Account acct1 = new Account(name='test Account One1');
        acct1.Type = 'Advertiser';
        insert acct1;
        
        
//Create Opportunity1 on Account
        Opportunity Oppty1 = new Opportunity(name='test Oppty One1');
        Oppty1.AccountId = acct1.Id;
        Oppty1.StageName = 'Test';
        Oppty1.CloseDate = Date.today();
        Oppty1.AED__Campaign_Start_Date__c = Date.today();
        Oppty1.AED__Campaign_Close_Date__c = Date.today()+1;
        Oppty1.AED__Invoice_Client_Name__c = acct1.Id;
        Oppty1.AED__Country__c = 'Spain';
        Oppty1.Campaign_Name_Agreement__c = 'test Oppty One1' ;
        Oppty1.Implementation_DirectTrack__c = 'Yes';
        Oppty1.Implementation_DT_Status__c = '1. Create Campaign';
        Oppty1.Approval_Status__c = 'Waiting for Approval';
       
        insert Oppty1;    
   
//Create Opportunity2 on Account
        Opportunity Oppty2 = new Opportunity(name='test Oppty One2');
        Oppty2.AccountId = acct1.Id;
        Oppty2.StageName = 'Test';
        Oppty2.CloseDate = Date.today();
        Oppty2.AED__Campaign_Start_Date__c = Date.today();
        Oppty2.AED__Campaign_Close_Date__c = Date.today()+1;
        Oppty2.AED__Invoice_Client_Name__c = acct1.Id;
        Oppty2.AED__Country__c = 'Spain';
        Oppty2.Campaign_Name_Agreement__c = 'test Oppty One2' ;
        Oppty2.Implementation_DirectTrack__c = 'No';
        Oppty2.Approval_Status__c = 'Waiting for Approval';   
        
        insert Oppty2;      
    
        test.starttest();    

        Oppty1.Approval_Status__c = 'Approved by AD'; 
        Oppty2.Approval_Status__c = 'Approved by AD';
        update Oppty1;
        update Oppty2;
        
        test.stoptest();
   
    }

}

 

 

Error is:

 

Failure Message: "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.CaseFromOpp.TriggersTest: line 22, column 9 External entry point"

 

Any suggestions?

 

Thanks,

 

Stefano

 

 

dmchengdmcheng

See the last  entry of this thread:

http://boards.developerforce.com/t5/General-Development/Unable-to-deploy-Code-quot-Duplicate-Username-quot/m-p/194982

 

Of course it doesn't make sense that you're not getting errors when testng in the sandbox.  However, try changing your Username and alias to a random string of characters and see if it works.

Cory CowgillCory Cowgill

Usernames have to be unique across all of Salesforce.

 

If someone in a completely different prodcution org has a username of "tester@test.com" you can't use it.

 

You can try appending a date/time stamp to the Username to make it unique everytime you run your test, or try adding some random numbers into the name like:

 

'tester123zzzUnitTest@test.com'

WizradWizrad

Thats a good point Cory.

 

Let's say I have a unit test that creates a user with username:

 

x@y.com

 

Now let's say...6 months later...someone registers a user with username x@y.com.

 

Will they have broken my unit test?