@istest private class Penalizacion_Memo_isTest {

@istest static void test_Memo(){
    user u = new user();
    u.LastName = 'Test Code';
    u.Email = '[email protected]';
    u.Alias = 'Tcode';
    u.Username = '[email protected]';
    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.


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


2 Answers 2


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

  • 3
    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
    Commented Apr 20, 2017 at 23:56

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 [email protected], try something like

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .