• Satish Nair 19
  • NEWBIE
  • 15 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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;
            }
        }
    
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;
            }
        }