0

We have a test class failing with the following error message:

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

As I understand, this can be resolved easily by changing the username (I'll likely adopt using a unique username as outlined here).

My question is why is this username is not unique? I checked – there's no instance of it in my production or any of our sandbox instances. If someone has this username in an instance that we don't manage (a different organization altogether), would it fail then? Or does it just check against production / sandbox instances that we manage? Thank you!

Test Class:

    @isTest
private class sendHTMLEmailTest {

    static testMethod void testUserEmail(){

        // Test with standard profile

        Profile p = [select id from profile where name='Standard User']; 

        User testUser = new User();
        testUser.email = 'test@test.com';
        testUser.Username ='test@test1234567890.com';
        testUser.alias ='testUser';
        testUser.TimeZoneSidKey = 'America/Chicago';
        testUser.localesidkey='en_US';
        testUser.emailencodingkey='UTF-8';
        testUser.languagelocalekey='en_US';
        testUser.localesidkey='en_US';
        testUser.ProfileId = p.Id;
        testUser.FirstName = 'Test';
        testUser.LastName = 'Test';
        testUser.Title = 'Some Person';
        testUser.IsActive = true;
        testUser.UserPermissionsInteractionUser = true;

        insert testUser;

        Test.startTest();

        // Test the email limits before running
        Integer emailbefore = Limits.getEmailInvocations();

        // Set all params save, email other addresses & save as activity

        sendHTMLEmailPlugin plugin = new sendHTMLEmailPlugin();
        Map<String, Object> inputParams = new Map<String, Object>();
        inputParams.put('htmlBody', '<strong>Hello world</strong>');
        inputParams.put('Subject', 'Hello world');
        inputParams.put('Target Object Id', testUser.Id);
        inputParams.put('Sender Address', testUser.Email);
        inputParams.put('Priority', 'High');

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = plugin.invoke(request);

        // Assert that the batch has decreased

        system.assertNotEquals(emailbefore,Limits.getEmailInvocations(),'should have decreased');

        Test.stopTest();
    }


    static testMethod void testLeadEmail(){

        // Test that it saves as an activity with the lead

        Lead testLead = new Lead();
        testLead.Salutation = 'Mr.';
        testLead.FirstName = 'Test';
        testLead.LastName = 'Test';
        testLead.Street = '12345 test Lane';
        testLead.City = 'Omaha';
        testLead.State = 'NE';
        testLead.PostalCode = '68135';
        testLead.Status ='Some Status';
        testLead.Company = 'Some Company';
        testLead.Country = 'USA';
        testLead.Email = 'test@test.com';
        testLead.Title = 'Some Title';

        insert testLead;

        Test.startTest();

        // Test the email limits before running
        Integer emailbefore = Limits.getEmailInvocations();

        // Set all params save, email other addresses

        sendHTMLEmailPlugin plugin = new sendHTMLEmailPlugin();
        Map<String, Object> inputParams = new Map<String, Object>();
        inputParams.put('htmlBody', '<strong>Hello world</strong>');
        inputParams.put('Subject', 'Hello world');
        inputParams.put('Target Object Id', testLead.Id);
        inputParams.put('Save As Activity', 'true');
        inputParams.put('Priority', 'Low');

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = plugin.invoke(request);

        // Assert that the batch has decreased

        system.assertNotEquals(emailbefore,Limits.getEmailInvocations(),'should have decreased');


        // Assert that the activity was saved

        List<Task> t = new List<Task>([SELECT Id FROM Task WHERE whoId = :testLead.Id]);
        system.assertEquals(1, t.size());

        Test.stopTest();
    }

    static testMethod void testContactEmail(){

        // Test that it saves as an activity with the user - test with standard profile

        // Create account for contact
        Account testAccount = new Account();
        testAccount.Name = 'Test';
        testAccount.BillingStreet = '12345 Test Lane';
        testAccount.BillingCity = 'Omaha';
        testAccount.BillingState = 'NE';
        testAccount.BillingPostalCode = '68135';
        testAccount.Type = 'Some Type';

        insert testAccount;

        // Create new contact
        Contact testContact= new Contact();
        testContact.Salutation = 'Mr.';
        testContact.FirstName = 'Test';
        testContact.LastName = 'Test';
        testContact.MailingStreet = '12345 test Lane';
        testContact.MailingCity = 'Omaha';
        testContact.MailingState = 'NE';
        testContact.MailingPostalCode = '68135';
        testContact.AccountId = testAccount.Id;
        testContact.MailingCountry = 'USA';
        testContact.Email = 'test@test.com';
        testContact.Title = 'Some Title';

        insert testContact;

        Test.startTest();

        // Test the email limits before running
        Integer emailbefore = Limits.getEmailInvocations();

        // Set all params save, email other addresses & save as activity

        sendHTMLEmailPlugin plugin = new sendHTMLEmailPlugin();
        Map<String, Object> inputParams = new Map<String, Object>();
        inputParams.put('htmlBody', '<strong>Hello world</strong>');
        inputParams.put('Subject', 'Hello world');
        inputParams.put('Target Object Id', testContact.Id);
        inputParams.put('Priority', 'High');
        inputParams.put('Save As Activity', 'true');
        inputParams.put('Sender Display Name', 'test@test.com');
        inputParams.put('CC Email Addresses (comma-separated)', 'test@test.com, test123@test.com');

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = plugin.invoke(request);

        // Assert that the batch has decreased

        system.assertNotEquals(emailbefore,Limits.getEmailInvocations(),'should have decreased');

        // Assert that the activity was saved

        List<Task> t = new List<Task>([SELECT Id FROM Task WHERE whoId = :testContact.Id]);
        system.assertEquals(1, t.size());

        Test.stopTest();
    }    

    static testMethod void testOtherEmail(){
        Test.startTest();

        // Test the email limits before running
        Integer emailbefore = Limits.getEmailInvocations();

        // Set all params save, email other addresses & save as activity

        sendHTMLEmailPlugin plugin = new sendHTMLEmailPlugin();
        Map<String, Object> inputParams = new Map<String, Object>();
        inputParams.put('htmlBody', '<strong>Hello world</strong>');
        inputParams.put('Subject', 'Hello world');
        inputParams.put('Other Emails (comma-separated)', 'Test123@test.com, test345@test.com');
        inputParams.put('Priority', 'High');

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = plugin.invoke(request);

        // Assert that the batch has decreased

        system.assertNotEquals(emailbefore,Limits.getEmailInvocations(),'should have decreased');

        Test.stopTest();
    }

    // Tests the describe method
    static testMethod void describeTest() {

        sendHTMLEmailPlugin plugin = new sendHTMLEmailPlugin();
        Process.PluginDescribeResult result =  plugin.describe();

        // Assert there are 10 inputs        
        System.AssertEquals(result.inputParameters.size(), 10);

    }
}
1

The article you link to - How to resolve DUPLICATE_USERNAME error returned while performing a deployment - includes the core explanation:

Usernames 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*)

When a user logs in, they don't login to a specific instance e.g. na25 or eu2 but login to https://logic.salesforce.com (or https://test.salesforce.com) and are routed to the correct instance based on their username. Hence the username must unique not just to your org or instance but across the whole environment (production or sandbox). The platform carefully avoids the possibility of duplicate usernames being created by validating usernames when they are inserted or updated.

This makes sense for real usernames, but it is less clear why the constraint exists in tests where all DML is discarded once a test runs. But that may relate to existing User objects not being isolated from tests - see Isolation of Test Data from Organization Data in Unit Tests.

  • Thank you! I wasn't sure whether their reference of "instance" applied to instances that we don't own. That's much clearer! – Carolyn Jan 6 '17 at 23:47
0

The username is most likely being used in another organization instance. I would recommend updating the username to another username.

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.