function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kabukabu 

How to check duplicate reords using multiple keys?What is the best approach?

Hi ,

I am looking at apex code to check thro triggers check if the record already exists  and display an error.

 

Suppose on Contact record  wanted to check  if Name+ email+phone exists then display an error to the user that duplicate record.

 

Is there any sample code anyone can share with me?

 

thanks

K

dphilldphill

I don't have sample code, but I can tell you how to go about it.

You need to use a dynamic query and go through al

I don't have sample code, but I can tell you how to go about it.

 

You need to use a dynamic query and go through all of your records (contacts in this case)

Something similar to:

String where = '';
for (Contact cont: contacts)
{
  where += ' OR (Email = ' + cont.Email + ' AND Phone = ' + cont.Phone + ' AND Name = ' + cont.Name + ' AND Id != ' + cont.Id;
}
//This should give a general idea on how to build your WHERE part of the query you will need to do.