You need to sign in to do that
Don't have an account?
Sameer Vits
check for existing contacts using email address
We have a requirement where we want to check if contact exists using email address custom field. If contact exists then we just want to update incoming information. If contact doesn't exists then it should create a new contact. Also, is apex a better solution or workflows?
The concept here is, I kept the very recent record that came in and delete all the old ones keeping the perspective the recent record with latest information. Let me know if that works
All Answers
Apex is the better Solution it's not possible using WF.
Below trigger is used for your scenario here am using the Standard Email field so use your custom field instead std field.
trigger UpsertDuplicate on Contact (After insert) {
Set<Id> DuplicateConToDelId = new Set<Id>();
Map<String,Contact> ConMap = new Map<String,Contact>();
List<Contact> ContacttoUpdate = new List<Contact>();
List<Contact> DuplicateContactestoDel = new List<Contact>();
for(Contact con:Trigger.new)
{
If(con.Email != null)
ConMap.put(con.Email,con);
}
List<Contact> ExistingContact = new List<Contact>();
ExistingContact = [Select Id,FirstName,LastName,Email,Phone,MobilePhone from Contact where Email IN : ConMap.keySet()];
if(ExistingContact.size() > 0)
{
for(Contact newcon : Trigger.New)
{
for(Contact excon : ExistingContact)
{
If(excon.Email == newcon.Email && ConMap.containsKey(excon.Email))
{
excon.FirstName = newcon.FirstName;
excon.LastName = newcon.LastName;
excon.Phone = newcon.Phone;
excon.MobilePhone = newcon.MobilePhone;
ContacttoUpdate.add(excon);
DuplicateConToDelId.add(newcon.Id);
}
}
}
}
If(ContacttoUpdate.size() > 0)
{
Update ContacttoUpdate;
}
If(DuplicateConToDelId.size() > 0)
{
DuplicateContactestoDel = [Select Id,Name from Contact where Id IN : DuplicateConToDelId];
If(DuplicateContactestoDel.size() > 0)
Delete DuplicateContactestoDel;
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
If(DuplicateConToDelId.size() > 0)
{
DuplicateContactestoDel = [Select Id,Name from Contact where Id IN : DuplicateConToDelId];
If(DuplicateContactestoDel.size() > 0)
Delete DuplicateContactestoDel;
}
Thanks.
Raj
The concept here is, I kept the very recent record that came in and delete all the old ones keeping the perspective the recent record with latest information. Let me know if that works