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
kdaviskdavis 

Trying to match phone number on SOSL but works

Hi,

 

I'm writing a trigger to prevent duplicate accounts from being created and to do that I am using a SOSL to search across phone fields for accounts. Person Accounts are being used. Its running on update and insert. I have no idea why but it will actually work on some instances but most of the time it allows duplicates. When you enter a value in the phone field in should search across all phone fields in all accounts to see if one exists. Code is below, please lemme know if you notice something becuase I am stumped. I limit the trigger to single updates only.

Thanks.

/*Need to limit the deduping from running for bulk updtes*/
    Integer triggerSize = Trigger.New.size();
    if(triggerSize == 1){ 
        if((Trigger.isUpdate && Trigger.isBefore) ||(Trigger.isInsert && Trigger.isAfter)){
            String phones;
            Account acct = Trigger.New[0];
            Account returnedAccount;
            String home = acct.PersonHomePhone;
            String mobile = acct.PersonMobilePhone;
            String otherPhone = acct.PersonOtherPhone;
            String regPhone = acct.Phone;
            Set<String> currPhones = new Set<String>{home,mobile,otherPhone,regPhone};
            Set<String> cleansedPhones = new Set<String> ();
            
            for(String p : currPhones){
                if(p != null){
                    p = p.replace('(',''); p = p.replace(')','');
                    p = p.replace('-',''); p = p.trim(); //remove characters               
                    p = p.replace(' ','');
                    cleansedPhones.add(p);
                }
            }

            for(String p : cleansedPhones){
                if(phones == null){
                    phones = String.ValueOf(p);
                }else{
                    phones = phones + ' OR '+String.ValueOf(p);
                }
            }


            System.debug('phones  '+phones);
            
            if(phones != null){
                String queryString = 'FIND {FILTER} IN PHONE FIELDS RETURNING Account(Owner.Name, Name,Id WHERE Id != \'currentId\')';      
                queryString = queryString.replace('FILTER',phones);
                queryString = queryString.replace('currentId',acct.Id); 
                System.debug('This is the Query String    '+queryString);           

                List<List <sObject>> query = search.query(queryString);
                System.debug('This is the return    '+query);
                for(sObject s : query[0]){
                    returnedAccount = (Account)s;
                    System.debug('This was returned  '+returnedAccount);
                }

                if(returnedAccount != null){
                    acct.addError('ERROR: This Account belongs to another broker: '+returnedAccount.Owner.Name, false);
                }
            }
        }
    }       

 

kdaviskdavis

The issue arises for updates a matter of 10 seconds to a minute apart. The SOSL doesn't identify the updated value on the dupe account until about a minute after