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
Matt MetrosMatt Metros 

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: []

@isTest(SeeAllData=true)
private class NumberOfAccountsRelatedToUserTest {
	
	@isTest static void test_method_one() {
		Integer num = 5;
		List<Account> accounts = createAccounts(num);
		List<Contact> contacts = createContacts(num, accounts);

		List<Account> accounts2 = createAccounts(num);
		List<Contact> contacts2 = createContacts(num, accounts2);

		User u  = createUser('myname', 'myname');
		User u2 = createUser('Test', 'Test');

		System.runAs(u){
			insert accounts;
			insert contacts;
		}

		System.runAs(u2){
			insert accounts2;
			insert contacts2;
		}

		Test.startTest();

			for(Account acc: accounts){
				acc.Account_Notes__c = 'Test Update';
				acc.SDR_Owner__c = u2.ID;
			}	

			update accounts;




		Test.stopTest();


	}


	public static User createUser(String username ,String theAlias){
		User u = new User(
			Alias = theAlias,
			Email = username + '@test.com',
			FirstName = 'Joe',
			LastName = username,
			TimeZoneSidKey = 'America/Los_Angeles',
			UserName = username + '@test.com',
			LocaleSidKey = 'en_US',
			EmailEncodingKey = 'UTF-8',
			LanguageLocaleKey = 'en_US'
			);

		u.profileID = userinfo.getProfileId();


		return u;
	}

	public static List<Account> createAccounts(Integer numOfAccounts){
		list<Account> accountsToInsert = new List<Account>();

		for(integer i = 0; i < numOfAccounts; i++){
			Account newAccount = new Account(
				Name = 'Test' + String.valueOf(i),
				Industry = 'Accounting',
				of_Locations__c = 5 + i
				);

			accountsToInsert.add(newAccount);

		}


		return accountsToInsert;
	}

	public static List<Contact> createContacts(Integer numOfContacts, List<Account> accounts){

		list<Contact> contactsToInsert = new List<Contact>();

		for(Integer i = 0; i < numOfContacts; i++){
			Contact newCon = new Contact (
				lastName 	= 'Test' + String.valueOf(i) ,
				accountId 	= accounts[i].ID ,
				title 		= 'Test' ,
    			email 		= String.valueOf(i) + 'test@test.com'
				);
			contactsToInsert.add(newCon);
		}

		return contactsToInsert;
	}
	
	
}

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: []

Class.NumberOfAccountsRelatedToUserTest.test_method_one: line 22, column 1

How on earth am I creating a duplicate record???
Lokesh KumarLokesh Kumar
Modify the test class annotation to 
@isTest(SeeAllData=false)

 
Raj VakatiRaj Vakati
This is the code 
 
@isTest(SeeAllData=false)
private class NumberOfAccountsRelatedToUserTest {
	
	@isTest static void test_method_one() {
		Integer num = 5;
		List<Account> accounts = createAccounts(num);
		List<Contact> contacts = createContacts(num, accounts);

		List<Account> accounts2 = createAccounts(num);
		List<Contact> contacts2 = createContacts(num, accounts2);

		User u  = createUser('myname'+System.now(), 'myname213');
		User u2 = createUser('Test',+System.now() 'Test5647465');

		System.runAs(u){
			insert accounts;
			insert contacts;
		}

		System.runAs(u2){
			insert accounts2;
			insert contacts2;
		}

		Test.startTest();

			for(Account acc: accounts){
				acc.Account_Notes__c = 'Test Update';
				acc.SDR_Owner__c = u2.ID;
			}	

			update accounts;




		Test.stopTest();


	}


	public static User createUser(String username ,String theAlias){
		User u = new User(
			Alias = theAlias,
			Email = username + '@test.com',
			FirstName = 'Joe',
			LastName = username,
			TimeZoneSidKey = 'America/Los_Angeles',
			UserName = username + '@test.com',
			LocaleSidKey = 'en_US',
			EmailEncodingKey = 'UTF-8',
			LanguageLocaleKey = 'en_US'
			);

		u.profileID = userinfo.getProfileId();


		return u;
	}

	public static List<Account> createAccounts(Integer numOfAccounts){
		list<Account> accountsToInsert = new List<Account>();

		for(integer i = 0; i < numOfAccounts; i++){
			Account newAccount = new Account(
				Name = 'Test' + String.valueOf(i),
				Industry = 'Accounting',
				of_Locations__c = 5 + i
				);

			accountsToInsert.add(newAccount);

		}


		return accountsToInsert;
	}

	public static List<Contact> createContacts(Integer numOfContacts, List<Account> accounts){

		list<Contact> contactsToInsert = new List<Contact>();

		for(Integer i = 0; i < numOfContacts; i++){
			Contact newCon = new Contact (
				lastName 	= 'Test' + String.valueOf(i) ,
				accountId 	= accounts[i].ID ,
				title 		= 'Test' ,
    			email 		= String.valueOf(i) + 'test@test.com'
				);
			contactsToInsert.add(newCon);
		}

		return contactsToInsert;
	}
	
	
}

 
Japleen KaurJapleen Kaur
Hi Matt,
I completely agree with Raj. It may be possible that you are creating 2 users under the same "userName". 
OR
"Duplication Rules" might be active in your org. 
Please feel free to ask if need any help in it.

Hope it helps. :)
Please mark the answer as Best Response to help others also find the right solution.
 
Aniruddha KshirsagarAniruddha Kshirsagar
I am practising triggers facing the same issue :- 

trigger AccountTrigger on Account (before insert, before delete) {
    
    if (trigger.isdelete && trigger.isbefore){
        
        for (Account acc:trigger.old){
            
            acc.adderror('You Cannot Delete the Account Record');
        }
    }
    
    if (trigger.isbefore && trigger.isinsert ){
        for (Account acc:trigger.new)
        {
            if (string.isBlank(acc.BillingCity))
                
                acc.BillingCity='NY';
            
        }
        
        
        
    }
}


Enonomus window code:- 

Account obj1= new Account();
obj1.name='Test New 102';
insert obj1;

Please help