You need to sign in to do that
Don't have an account?
Rajesh-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
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
use below trigger code for bulk data upload
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
use below trigger code for bulk data upload
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
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