Bypass Account Apex Validation Rule during Lead Conversion

apexlead-conversion

In Lead Setting we have Require Validation for Converted Leads set to true. I have an existing method on Before Insert & before update of Account that displays validation message using apex. Reason that we used apex validation instead of config validation was because order of execution.

Requirements:

We are implementing solution where whenever user add Phone number, we introduced Picklist field called as Country Code which is required during record creation and record update. Now In Salesforce Phone field there's no such strong validation like it allows you to add characters in phone number field itself. You can add as no. of digits as you want. So I have added certain apex validation before create/update of record that checks Phone Number should not contain digit, It allows only + sign as special character, Phone should be greater than 4 digits or less than 15 digits, Phone Country Code is required when adding phone number. So once all validation gets bypassed, we are mapping Country Code with Phone No.

e.g. If Country Code(Picklist) is United States +1 and phone no. 12345678 then after saving the record, it display Phone Number as +1 12345678.

Below is the code that I have written on Before Insert & Before Update:

Code:

public void autoPopulateCountryCode(List<SObject> newItems,Map<Id, SObject> mapOfOldItems) {
    Set<String> contactId_Set = new Set<String>();
    Pattern MyPattern;
    Matcher MyMatcher;
    for(SObject newObj:newItems){
        Account newAccountObj = (Account)newObj;
        Account oldAccountObj = mapOfOldItems!= null ? (Account)mapOfOldItems.get(newObj.Id):null;
        if(newAccountObj.Phone != null  && (Trigger.isInsert || (Trigger.isUpdate && (newAccountObj.Phone != oldAccountObj.Phone ||  newAccountObj.Country_Code__c != oldAccountObj.Country_Code__c)))){
            String regex = '[0-9\\+/\\s/g]+'; // expression that allow numbers,White spaces and + sign.
            MyPattern = Pattern.compile(regex);
            MyMatcher = MyPattern.matcher(newAccountObj.Phone);
            if((Trigger.isInsert ||Trigger.isUpdate) && !MyMatcher.matches()){
                newAccountObj.addError('Phone Number should nt contais digit or special charater except + sign');
                break;
            }
            else if(!(newAccountObj.Phone.length() >= 5 && newAccountObj.Phone.length()<=16)){
                newAccountObj.addError('Phone Number should be greater than 5 an dless than 16');
                break;
            }
            else if(String.isBlank(newAccountObj.Country_Code__c)){
                newAccountObj.addError('Please Select Country Code');
                break;
            }  
            // If all Validation bypass then Concatinating Country Code with Phone Number.
            Map<String,Country_Code_Mapping__c> mapOfCountryCode = PhoneNumberUtil.autoPopulateCountryPhoneCode(); //Fetch All Country Code Data.
            if(!mapOfCountryCode.isEmpty()){
                if(newAccountObj.Phone != null && (Trigger.isInsert || (Trigger.isUpdate && newAccountObj.Phone != oldAccountObj.Phone && !newAccountObj.Phone.contains(mapOfCountryCode.get(newAccountObj.Country_Code__c).Mobile_Code__c)))){
                    newAccountObj.Phone = mapOfCountryCode.get(newAccountObj.Country_Code__c).Mobile_Code__c + ' ' + newAccountObj.Phone.remove(mapOfCountryCode.get(newAccountObj.Country_Code__c).Mobile_Code__c).replaceAll( '\\s+', '');
                }
                else if(Trigger.isUpdate && newAccountObj.Country_Code__c != oldAccountObj.Country_Code__c){
                    newAccountObj.Phone = mapOfCountryCode.get(newAccountObj.Country_Code__c).Mobile_Code__c + ' ' + newAccountObj.Phone.remove(mapOfCountryCode.get(oldAccountObj.Country_Code__c).Mobile_Code__c).replaceAll( '\\s+', '');
                }
            }
        }
    }
}

Now when I convert the Lead into Account, method is executed and throws validation message on screen. I want to bypass the method on before insert/update of Account during lead conversion.
I tried to put some Boolean check but it's not working because of Order of execution in Lead Conversion as Account Trigger calls first.

Is there any condition that I can use in my Account before insert/update method that bypasses the logic automatically during Lead conversion?

Best Answer

You can detect this condition in an after insert/after update trigger. However, your validations in a before insert/before update DML event would need to be moved to an after insert/after update DML event.

Boolean isBeingConverted = [SELECT COUNT() FROM Lead WHERE ConvertedAccountId = :Trigger.new] > 0;

Note that addError still works in an after insert/after update DML event, but without knowing exactly what validations you're doing in the before DML event, it's impossible to provide certainty that this solution will work for you.

Related Topic