0

I am learning Apex Development. We have a Developer interview and we are planning to ask him/her to code for the below-mentioned question.

> For a given contact record, check if there is an existing contact that
> matches first name + last name + email address?

Can somebody show me how the code should look like and how can we check the solution for this?

Thanks!!

3
  • 1
    Are you sure you're not the "developer" being interviewed? Why would you/your company ask a question that you don't know the solution for, especially such a simple question as this? Commented Feb 21, 2017 at 17:38
  • @ Brian: Hi, We recently started using Salesforce. And the team is small so they are hiring admins, developers, tech architects etc. We saw a lot of developers resumes but many didn't come because of code test. We were not sure when a person says he is a developer why should he not come for code test. So we need someone who is really strong in coding and can work on tasks without any dependancy as it's a small team as of now. Commented Feb 21, 2017 at 19:02
  • @LMK Asking for a Trailhead resume (show me your badges) might be more productive. Or ask for certs; you can verify online. Or even a simple FizzBuzz should be enough to figure out if the developer at least understands how to write algorithms. Any developer that can figure out FizzBuzz can probably figure out something as specific as finding duplicates by first, last, and email.
    – sfdcfox
    Commented Feb 22, 2017 at 15:29

2 Answers 2

0

The most basic, straightforward, and quickest solution would be:

public class ContactMatch {
    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);
    }
}

Save the above class as an Apex class. 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. You can paste the script below in EA, and check the "open logs" option before pressing "run." then check the "Debug only" option once the log is open, and you'll see the Debug code indicating if there was a match found in the database or not. To check different names/emails, substitute different values in for the testContact parameters.

Contact testContact = new Contact(
    FirstName = 'testFirstName',
    LastName = 'testLastName',
    Email = 'testEmail'
);
System.debug('Match found: ' + ContactMatch.match(testContact));

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.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Commented Feb 22, 2017 at 16:02
1

I've written prior - examples of this on this site and others.

That said, this is 2017. A developer shouldn't have to answer that question: simply create some Duplicate Rules and move on. Any proper developer would first ask why a trigger is necessary when there's a standard feature that does the same thing.

As far as testing it, log them into a sandbox, tell them to write it, and then test it. It should be pretty easy to test, and it shouldn't take more than about 10 minutes to write. Or, you could pre-write a unit test and tell them to make it pass. For example:

@isTest static void test() {
    Contact c1 = new Contact(LastName='Test',FirstName='Test',Email='[email protected]'),
            c2 = c1.clone();
    insert c1;
    Database.SaveResult sr = Database.insert(c2, false);
    System.assertEquals(false, sr.isSuccess());
}

Obviously, you might write several scenarios, including more than one record in a context, etc. The point is, you want to provide an easy automated way to check if the solution works.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .