You need to sign in to do that
Don't have an account?
Dipthi
Trigger preventing duplicates - Plz help..I am struck
trigger PreventDuplicateOnContact on Contact (before insert) {
// Set to store Phone No's
Set<String> ContactPhoneSet = new Set<String> ();
// Set to store Email ID's
Set<String> ContactEmailSet = new Set<String> ();
// Iterate through each new Contact and add their email and Phone in to Sets
for(Contact Con : Trigger.new) {
ContactPhoneSet.add(con.Phone);
ContactPhoneSet.add(con.Email);
}
List<Contact> ContactPhoneList = new List<Contact>();
List<Contact> ContactEmailList = new List<Contact>();
ContactEmailList = [Select email From Contact Where Email IN : ContactEmailSet];
ContactPhoneList = [Select Phone From Contact Where Phone IN : ContactPhoneSet];
for(Contact Con : Trigger.new) {
If(ContactEmailList.size() > 0) {
Con.Email.AddError('Duplicate Contact Found With Same Email');
System.debug('Email');
}
If(ContactPhoneList.size() > 0) {
Con.Phone.AddError('Duplicate Contact Found With Same Phone');
}
}
}
// Set to store Phone No's
Set<String> ContactPhoneSet = new Set<String> ();
// Set to store Email ID's
Set<String> ContactEmailSet = new Set<String> ();
// Iterate through each new Contact and add their email and Phone in to Sets
for(Contact Con : Trigger.new) {
ContactPhoneSet.add(con.Phone);
ContactPhoneSet.add(con.Email);
}
List<Contact> ContactPhoneList = new List<Contact>();
List<Contact> ContactEmailList = new List<Contact>();
ContactEmailList = [Select email From Contact Where Email IN : ContactEmailSet];
ContactPhoneList = [Select Phone From Contact Where Phone IN : ContactPhoneSet];
for(Contact Con : Trigger.new) {
If(ContactEmailList.size() > 0) {
Con.Email.AddError('Duplicate Contact Found With Same Email');
System.debug('Email');
}
If(ContactPhoneList.size() > 0) {
Con.Phone.AddError('Duplicate Contact Found With Same Phone');
}
}
}
trigger PreventDuplicateOnContact on Contact (before insert) {
// Set to store Phone No's
Set<String> ContactPhoneSet = new Set<String> ();
// Set to store Email ID's
Set<String> ContactEmailSet = new Set<String> ();
// Iterate through each new Contact and add their email and Phone in to Sets
for(Contact con : Trigger.new) {
ContactPhoneSet.add(con.Phone);
ContactEmailSet.add(con.Email);
// New List to store email and Phone numbers
List<Contact> ContactPhoneList = [ SELECT Phone FROM Contact WHERE Phone IN :ContactPhoneSet];
List<Contact> ContactEmailList = [ SELECT email FROM Contact WHERE email IN :ContactEmailSet];
If(ContactPhoneList.size() > 0) {
con.Phone.adderror ('Duplicate Contact with same Phone');
}
If(ContactEmailList.size() > 0) {
con.Email.adderror ('Duplicate Contact with same Email');
}
}
}
All Answers
The above blog has a code snippet, you have to make changes based on your requirement.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
But can you please help me to display error message at the appropriate field (Phone/Email)
{
LIST<CONTACT> CD=TRIGGER.NEW;
FOR(CONTACT C:CD)
{
LIST<CONTACT> mylist=[SELECT ID,EMAIL FROM CONTACT WHERE EMAIL=:C.EMAIL ];
LIST<CONTACT> mylist1=[SELECT ID,PHONE FROM CONTACT WHERE EMAIL=:C.phone ];
IF(mylist.size() > 0)
{
C.EMAIL.ADDERROR('THIS EMAIL ID EXIST');
}
else if(mylist1.size() > 0)
{
C.phone.ADDERROR('THIS Phone number EXIST');
}
}
}
trigger PreventDuplicateOnContact on Contact (before insert) {
// Set to store Phone No's
Set<String> ContactPhoneSet = new Set<String> ();
// Set to store Email ID's
Set<String> ContactEmailSet = new Set<String> ();
// Iterate through each new Contact and add their email and Phone in to Sets
for(Contact con : Trigger.new) {
ContactPhoneSet.add(con.Phone);
ContactEmailSet.add(con.Email);
// New List to store email and Phone numbers
List<Contact> ContactPhoneList = [ SELECT Phone FROM Contact WHERE Phone IN :ContactPhoneSet];
List<Contact> ContactEmailList = [ SELECT email FROM Contact WHERE email IN :ContactEmailSet];
If(ContactPhoneList.size() > 0) {
con.Phone.adderror ('Duplicate Contact with same Phone');
}
If(ContactEmailList.size() > 0) {
con.Email.adderror ('Duplicate Contact with same Email');
}
}
}