• Piyush Pranav 17
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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);
                    }
                }
            }
        }
    }
}
 
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);
                    }
                }
            }
        }
    }
}