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
Rajesh-SFDCRajesh-SFDC 

Dublicate Email Detection Trigger

Hi Everyone , 

I am new to trigger i had one requirement to write a trigger for dublicate email detection in bulk mode .
I tried below code 

trigger DublicateEmailDetection on Candidate__c ( before insert,before update ) {
List<Candidate__c> EmailList = [Select Email__c from Candidate__c];
    Set<String> setString = new Set<String>();
    for (Candidate__c k : EmailList )
    {
     setString.add(k.Email__c);
    } 
    for(Candidate__c c : Trigger.new)
    {
    if(setString.contains(c.Email__c))
        {
        c.Email__c.addError('Duplicate Email');
        
        }
    }

Can you please check if this code will work fine in bulk mode ?
Youre help is really appreciated 
Best Answer chosen by Rajesh-SFDC
sfdcMonkey.comsfdcMonkey.com
hi rajesh kumar
use below trigger code for bulk data upload
 
trigger DublicateEmailDetection on Candidate__c ( before insert,before update ) {
   Map<String, Candidate__c> canMap = new Map<String, Candidate__c>(); 
   for (Candidate__c l : Trigger.new) {
    if ((l.Email__c != null) && (Trigger.isInsert || (l.Email__c != Trigger.oldMap.get(l.Id).Email__c))) {

        // Make sure another new lead isn't also a duplicate
        if (canMap.containsKey(l.Email__c)) {
            l.Email__c.addError('Another new record has the same email address.');
        } else {
            canMap.put(l.Email__c, l);
        }
    }
}
for (Candidate__c l : [SELECT Email__c FROM Candidate__c WHERE Email__c IN :canMap.KeySet()]) {
    Candidate__c newLCan = canMap.get(l.Email__c);
    newLCan.Email__c.addError('A record with this email address already exists.');
   }   
 }
addError() doesn't roll back your insertion but it prevent the further execution of the script, so you data is never inserted if an you throw an error on UI.
 
I would suggest you to first take all values in any map and check with database for duplicate values. If there are duplicate values then create another list and insert the rest others. Now using that list throw an error listing all names of duplicate values on UI.
thanks
Let me inform if it helps you and mark it best answer if it helps you so it make proper solution for others in future
thanks

 

All Answers

Rajesh-SFDCRajesh-SFDC
Just to update this code is not detecting dublicate when loading candidate through data loader with dublicate email id ..please help me on this
sfdcMonkey.comsfdcMonkey.com
hi rajesh kumar
use below trigger code for bulk data upload
 
trigger DublicateEmailDetection on Candidate__c ( before insert,before update ) {
   Map<String, Candidate__c> canMap = new Map<String, Candidate__c>(); 
   for (Candidate__c l : Trigger.new) {
    if ((l.Email__c != null) && (Trigger.isInsert || (l.Email__c != Trigger.oldMap.get(l.Id).Email__c))) {

        // Make sure another new lead isn't also a duplicate
        if (canMap.containsKey(l.Email__c)) {
            l.Email__c.addError('Another new record has the same email address.');
        } else {
            canMap.put(l.Email__c, l);
        }
    }
}
for (Candidate__c l : [SELECT Email__c FROM Candidate__c WHERE Email__c IN :canMap.KeySet()]) {
    Candidate__c newLCan = canMap.get(l.Email__c);
    newLCan.Email__c.addError('A record with this email address already exists.');
   }   
 }
addError() doesn't roll back your insertion but it prevent the further execution of the script, so you data is never inserted if an you throw an error on UI.
 
I would suggest you to first take all values in any map and check with database for duplicate values. If there are duplicate values then create another list and insert the rest others. Now using that list throw an error listing all names of duplicate values on UI.
thanks
Let me inform if it helps you and mark it best answer if it helps you so it make proper solution for others in future
thanks

 
This was selected as the best answer
Rajesh-SFDCRajesh-SFDC
Hi Piyush ,

Suppose we have 100 recorrds to insert and then in that many duplicates are there so first add error will recognize that and if there is not duplicate then it will check against database for each record if there is dublicate then second aderror method will recognize that .

Anyway your code worked ! 

Thanks a lot for your help! 

Regards!
Rajesh