You need to sign in to do that
Don't have an account?
Satish 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;
}
}
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;
}
}
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. 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
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. 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