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
Lakshmi Kumar 7Lakshmi Kumar 7 

Apex Code to check if there is an existing contact that matches first name + last name + email address


I am new and learning Apex Development. We have a Developer interview and we are planning to ask him/her to code for the below-mentioned question.Can somebody show me how the code should look like and how can we check the solution for this?
Question: For a given contact record, check if there is an existing contact that > matches first name + last name + email address?

NagendraNagendra (Salesforce Developers) 
Hi Lakshmi,

Please try the code below.
public static Boolean match(Contact c) {
    List<Contact> test = [SELECT FirstName, LastName, Email 
                           FROM Contact 
                           WHERE FirstName =: c.FirstName
                           AND LastName =: c.LastName 
                           AND Email =: c.Email
                           AND Id !=: c.Id];
    return (test.size() > 0);
To test it you could call the static match method from Execute Anonymous, with the "given Contact record" as the parameter. It would return true if there was a match, false if not.

If you're testing with a Contact record already in the database (as opposed to a Contact record you allocate in EA but do not insert), the Id !=: c.Id clause will prevent counting that record itself as a match.

Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Best Regards,
Lakshmi Kumar 7Lakshmi Kumar 7
@Nagendra: Thanks for the help. I tried executing via anonymous as you mentioned but I am getting an error. I opened execute anonymous window and typed static match katie; 

Am I not doing this right? We are testing with contact record that already exists in the DB.