Duplication check for Account Email

Trigger AccountTrigger on Account(before insert,before update){
set<String> setemail=new set<String>();
for(Account a:Trigger.new){
setemail.add(a.Email__c);
System.debug('values in the set'+setemail);

}
set<String> setdata=new set<String>();
    for(Account a:[select Id,Name,Email__c from Account where Email__c in:setemail]){
        setdata.add(a.Email__c);
       
    }
    for(Account a:Trigger.new){
        if(setdata.contains(a.Email__c)){
            a.addError('Enter unique value');
        }
    }}


Comments