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
Piyush Pranav 17Piyush Pranav 17 

Deduplication logic implementation with Trigger ?

I  am trying to use the following  deduplication logic with Trigger on Candidate object to prevent inserting duplicates based on a unique key . However this logic  even prevents  me from inserting the first entry too which is not a duplicate . not sure  what I am doing wrong. Please advise.

Trigger:
trigger CandidateKeyTrigger on Candidate__c (before insert,before update) {
    CandidateKeyClass.candidatededuplicationmethod(Trigger.new);
}

Class:
public class CandidateKeyClass {
    public static void candidatededuplicationmethod(List<Candidate__c> newcandidates){
        Map<String,Candidate__c> newcandidatemap = new Map<String,Candidate__c>();
        //check the batch for duplicates
        for(Candidate__c cand:newcandidates){
                if(newcandidatemap.containsKey(cand.Unique_Key__c)){
                    cand.adderror('Duplicate name+email found in batch');
                }
                else{
                    newcandidatemap.put(cand.Unique_Key__c,cand);
                }
        }
        //check the db for duplicates
        if(!newcandidatemap.isempty()){
            for(List<Candidate__c> cands:[Select unique_key__c from Candidate__c where unique_key__c in :newcandidatemap.keyset()]){
                for(Candidate__c can:cands){
                    if(newcandidatemap.containsKey(can.Unique_Key__c){
                        newcandidatemap.get(can.Unique_Key__c).adderror('duplicate unique key found in salesforce with id ' + can.id);
                    }
                }
            }
        }
    }
}
 
Best Answer chosen by Piyush Pranav 17
v varaprasadv varaprasad
Hi Piyush,

Please check once below sample code : 
 
public class CandidateKeyClass {
    public static void candidatededuplicationmethod(List<Candidate__c> newcandidates){
		set<string> setOfUniqueIds = new set<string>();
            for(Candidate__c cands:[Select unique_key__c from Candidate__c]){
                setOfUniqueIds.add(cands.unique_key__c);
            }
			
			//check the batch for duplicates
        for(Candidate__c cand:newcandidates){
                if(setOfUniqueIds.contains(cand.Unique_Key__c)){
                    cand.adderror('Duplicate name+email found in batch');
                }
               
           }
			
			
        }
    
}

Note: I have not tested above code.please let me know in case of any issues.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

All Answers

Raj VakatiRaj Vakati
Your code looks correct .. That record which you are inserting might be having duplicated in any existing data 
v varaprasadv varaprasad
Hi Piyush,

Please check once below sample code : 
 
public class CandidateKeyClass {
    public static void candidatededuplicationmethod(List<Candidate__c> newcandidates){
		set<string> setOfUniqueIds = new set<string>();
            for(Candidate__c cands:[Select unique_key__c from Candidate__c]){
                setOfUniqueIds.add(cands.unique_key__c);
            }
			
			//check the batch for duplicates
        for(Candidate__c cand:newcandidates){
                if(setOfUniqueIds.contains(cand.Unique_Key__c)){
                    cand.adderror('Duplicate name+email found in batch');
                }
               
           }
			
			
        }
    
}

Note: I have not tested above code.please let me know in case of any issues.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
This was selected as the best answer
Piyush Pranav 17Piyush Pranav 17
Raj V ,
This  does not work . This object  has very limited data  and there is no duplication .  It looks like  the first  record  is still  being prevented because of the following line of code .

for(List<Candidate__c> cands:[Select unique_key__c from Candidate__c where unique_key__c in :newcandidatemap.keyset()]){ //This part  is seemingly pulling in the record which is being passed in the Trigger too . If  I include an additional check as belows it works.

        //check the db for duplicates
        if(!newcandidatemap.isempty()){
            for(List<Candidate__c> cands:[Select unique_key__c from Candidate__c where unique_key__c in :newcandidatemap.keyset()]){
                for(Candidate__c can:cands){
              String currentrecordId = can.Id;
             if(newcandidatemap.containsKey(can.Unique_Key__c && currentrecordId !=newcandidatemap.get(can.Unique_Key__c).Id){
                        newcandidatemap.get(can.Unique_Key__c).adderror('duplicate unique key found in salesforce with id ' + can.id);
                    }
                }
            }
        }

What  I  am not  able to figure  out  is how the record which is  being passed in the Trigger  is being queried without being inserted ( as I  am using before Trigger)   and how the record ID  is also generated before  the record is actually inserted . Per my underdstanding Record Id  would only generate once the record is saved  .