0
@istest private class Penalizacion_Memo_isTest {

@istest static void test_Memo(){
    user u = new user();
    u.MEMO__c=TRUE;
    u.LastName = 'Test Code';
    u.Email = 'test@test.com';
    u.Alias = 'Tcode';
    u.Username = 'test12345@test.com';
    u.CommunityNickname = 'test12';
    u.LocaleSidKey = 'en_US';
    u.TimeZoneSidKey = 'GMT';
    u.ProfileID = '00e36000000ao2F';
    u.LanguageLocaleKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    insert u;
}
}

This is my code. I've already changed the username a couple of times and still receive the same error even though I'm absolutely positive there is no user with that username in any of our organizations.

Error:

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] Stack Trace: Class.Penalizacion_Memo_isTest.test_Memo: line 16, column 1

3

User name must be unique across all Salesforce organizations which Salesforce has created till now

Append Datetime.Now() with username String to make it unique

  • 2
    As an alternative, since using "now" could possibly result in duplicates (say, if everyone adopted this practice), consider using the CSPRNG we have available: String username = Crypto.getRandomLong()+'@'+Crypto.getRandomLong()+'.abc'; or something like that. Anything to make it arbitrarily difficult to run into any other random value that might appear. – sfdcfox Apr 20 '17 at 23:56
1

User name is unique across all orgs. Random usernames that has test domains and 12345 numerics might be an issue sometimes. I faced the similar issue in the past. Its better to build emails and usernames in such a way that they are unique and chance of possible duplication is low. I prefer appending with company name as domain or adding system.now() or current time at the end. If its a user name, you can add sandbox name as well.

Instead of test@test.com, try something like

u.Username = testuser@mycompnay+string.valueof(system.now());

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.