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
brahmanaidubrahmanaidu 

how to restrict the user to create duplicate records for an account record with the help of trigger.Can some Please send me the code for this

sfdcMonkey.comsfdcMonkey.com
try following trigger :
trigger preventDup on Account(before insert, before update){

map<Id,Account> existingAccountMap = new  map<Id,Account>([Select Id, Name, Rating From Account]); 

   for(account acc : trigger.new){
      
	   if(acc.name = existingAccountMap.get(acc.Id).Name){
	   
          acc.adderror('You cannot create a duplicate account');
        }
		
   }
}
thanks let us know if it helps you
 
brahmanaidubrahmanaidu
User should not able to create a duplicate contact record for an account object
Bhargavi TunuguntlaBhargavi Tunuguntla
Actually Duplicate contacts will not be created to Account.If in case, you can try the below trigger code:
 
trigger DuplicateContactOnAccount on Contact (before insert,before Update) {
    Set<id> AccountIds=new set<id>();
   Map<String,String> extMap=new Map<String,String>();
for(Contact c : Trigger.new){
     AccountIds.add(c.AccountId);
 }
 for(Account a : [select Id, Name from Account where Name IN :AccountIds]){
            extMap.put(a.Id, a.Name);
 }
 for(Contact c : Trigger.new){
    
     if(extMap.containsKey(c.AccountId))
     {
         c.adderror('Contact is already added to Account');
     }
 }
}