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
SrinivasSrinivas 

Avoid Duplication

I have twenty account records.

Account has three fields name,type,industry 

if one record name,type and industry is same with other record and that other record should not be inserted ( while Inserting all the twenty records, it should check). It should thrown an error .
How can i write a trigger . Any help 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Srinivas,

Can you check the below trigger logic works for you.
trigger AccountDuplicate on Account (before insert)
{

    Set<String> setName = new Set<String>();
    Set<String> Ratingset= new set<String>();
    Set<String> Industry= new set<String>();
    For(Account acc : trigger.new)
    {
        system.debug('into set');
        setName.add(acc.name);
        Industry.add(acc.Industry);
        

    }
    
    if(setName.size() > 0 )
    {
        List<Account> lstAccount = [select name ,id,rating,Industry from account where name in :setName and Industry in :Industry ];
        
        Map<String ,Account> mapNameWiseAccount = new Map<String,Account>();
        For(Account acc: lstAccount)
        {
            mapNameWiseAccount.put(acc.name ,acc);
        }
        
        For(Account acc : trigger.new)
        {
            system.debug('into for');
            system.debug('industry'+acc.Industry);
            system.debug('industry'+mapNameWiseAccount.get(acc.name).Industry);
            if(mapNameWiseAccount.containsKey(acc.name) && acc.Rating==mapNameWiseAccount.get(acc.name).rating )
            {
                        system.debug('into error');

                acc.Name.addError('Name already Exist ');
            }
        }
        
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
FM WAFM WA
This is such a great post, and was thinking much the same myself. Another great update. https://www.fmwa.me/
CharuDuttCharuDutt
Hii Srinivas
Try Below Code
trigger PreventDuplicateAccounts on Account (before insert) {
      Set <String> NameSet = new Set<String>(); 
      Set <String> TypeSet = new Set<String>(); 
      Set <String> IndustrySet = new Set<String>(); 
    for (Account Acc :trigger.new) {
if(Acc.Name!= null){        
NameSet.add(Acc.Name);
}
if(Acc.Type!= null){        
TypeSet.add(Acc.Type);
}
if(Acc.Industry!= null){        
IndustrySet.add(Acc.Industry);
}
        
    }
    List <Account> AccountList = [SELECT Id,Name,Type,Industry FROM Account WHERE Name IN :NameSet OR Type IN :TypeSet OR Industry IN :IndustrySet];

    for (Account Acc : Trigger.new) {
        If (AccountList .size() > 0) {
            
          Acc.Email.adderror( 'Duplicate Accou ntFound.' );
            
        }
    }
]
Please Mark It As Best Answer If It Helps
Thank You!