You need to sign in to do that
Don't have an account?
Piyush 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);
}
}
}
}
}
}
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);
}
}
}
}
}
}
Please check once below sample code :
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
Please check once below sample code :
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 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 .