+ Start a Discussion
Satish Nair 19Satish Nair 19 

Duplicate contact check - Custom Visualforce

I have a custom visualforce page to create a contact record. It has some input fields like FirstName, LastName,Email,Phone to be entered. 
However,there are some permutation combination to be made to check if the entered contact already exists.
Ex: If firstname, lastname and email matches then throw an error: duplicate contact prevented.
I have written a piece of code which works fine and it does all the necessary checks. but my concern is if I deploy to production where I will have large number of contacts then it will hit the "governor limits" . Please help me in optimizing the code.

private Boolean duplicateContact() {
        Boolean error = false;
        String regExp = '[()-]';
        String replacement = PCS_UTIL_Constants.BLANK;
        String oldphone = PCS_UTIL_Constants.BLANK;
        String newphone = phone!=null ? phone.replaceAll(regExp,replacement) : PCS_UTIL_Constants.BLANK;
        for(Contact  existConList : [Select Id, firstName, lastName,phone, email from Contact limit 49000]) {
            oldphone = existConList.phone!=null? existConList.phone.replaceAll(regExp,replacement): PCS_UTIL_Constants.BLANK;

             if(((!String.isBlank(lname) && lname.equalsIgnoreCase(existConList.lastname)) &&
               ((!String.isBlank(email) && email.equalsIgnoreCase(existConList.email)) ||
               (existConList.phone!=null && newphone == oldphone)))||
               (!String.isBlank(lname) && !lname.equalsIgnoreCase(existConList.lastname))&&
                ((!String.isBlank(email) && email.equalsIgnoreCase(existConList.email))||
                (existConList.phone!=null && newphone == oldphone))){
                    error = true;
            }
        }
    
Best Answer chosen by Satish Nair 19
anto nirmalanto nirmal
Hi Satish,

Instead of Fetching all the records and doing the business logic in all the records, Please add the business logic in your SOQL itself so that it retreives only if any relevant record is available.
Something similar to the below.
SELECT Id, firstName, lastName,phone, email FROM Contact WHERE firstName = oldFirstFame AND  lastName = oldFirstFame AND email = oldEmail LIMIT 49000
If you replace yout existing query with this, all you need to do is a if contition and if a record is available as an output of the query, you just need to mark the error.

This is just for the basic duplicate check you have asked for.
If you need to add more funtionality the solution might differ.
Alternatively you could use SOSL as well based on the business logic needed.

Let me know if that helps.

As a common practice, if your question is answered, please choose 1 best answer.
Additionally you can give every answer a like if that answer is helpful to you.

Regards,
Anto Nirmal

All Answers

anto nirmalanto nirmal
Hi Satish,

Instead of Fetching all the records and doing the business logic in all the records, Please add the business logic in your SOQL itself so that it retreives only if any relevant record is available.
Something similar to the below.
SELECT Id, firstName, lastName,phone, email FROM Contact WHERE firstName = oldFirstFame AND  lastName = oldFirstFame AND email = oldEmail LIMIT 49000
If you replace yout existing query with this, all you need to do is a if contition and if a record is available as an output of the query, you just need to mark the error.

This is just for the basic duplicate check you have asked for.
If you need to add more funtionality the solution might differ.
Alternatively you could use SOSL as well based on the business logic needed.

Let me know if that helps.

As a common practice, if your question is answered, please choose 1 best answer.
Additionally you can give every answer a like if that answer is helpful to you.

Regards,
Anto Nirmal
This was selected as the best answer
anto nirmalanto nirmal
Hi Satish, Was that Helpful?
Satish Nair 19Satish Nair 19
Thanks Anto, that was indeed helpful!!!