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
SFDummySFDummy 

Business requires to have duplicate account name - Alternative solution to prevent duplicates?

Hello,
Our business requires us to have duplicate account names. Any suggestions on how to create unique field that will allow us to use like Account Name (like in reports)
For example we need to create contracts with "ABC" company that purchases 4 plans from us for each location. all four contracts need to be under ABC name. It might be due to different renewal dates.

Thanks
Uma
Harshit Garg 6Harshit Garg 6
HI SFDummy,

Please use below code.

trigger AccountDuplicate on Account (before insert)
{

	Set<String> setName = new Set<String>();
	For(Account acc : trigger.new)
	{
		setName.add(acc.name);
	}
	
	if(setName.size() > 0 )
	{
		List<Account> lstAccount = [select name ,id from account where name in :setName ];
		
		Map<String ,Account> mapNameWiseAccount = new Map<String,Account>();
		For(Account acc: lstAccount)
		{
			mapNameWiseAccount.put(acc.name ,acc);
		}
		
		For(Account acc : trigger.new)
		{
			if(mapNameWiseAccount.containsKey(acc.name))
			{
				acc.Name.addError('Name already Exist ');
			}
		}
		
	}
}


If that code helps you..Please choose my ans as the best ans.



SFDummySFDummy
Thanks Harshit Garg 6 for the quick and detailed response. I do not want to prevent users from creating account with duplicate name. I think that is what the sample code you provided is doing.  
I want to create another field that is unique, that has similar features like account Name