1

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?

2 Answers 2

1

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.

2
  • Hi @sfdcfox, I have added requirement along with code. My only concern is during Lead conversion to Account I want to bypass all those validations. Now if I try to add the same After Insert, I have to update the same record(Country Code+Phone No.) which is why I chose Before Trigger Instead of After
    – SFDCDJ
    Feb 8, 2022 at 10:08
  • @sfuser Personally, I think you could still move the validations to the after insert/update triggers, and leave the bit where you fix the phone info in a before insert/update trigger. It's a bit backwards from conventional wisdom, but I see no reason why it couldn't work.
    – sfdcfox
    Feb 8, 2022 at 13:04
0

You can do this as follows:--

  1. Replace standard convert button with a custom button.
  2. Click button to call flow to call apex.
  3. Do the logic you need to skip the methods in Account trigger (like using a static variable).
  4. Use convertLead() method on database class to convert lead.
3
  • How do I make this flag to true during convert operation? There's no such option on Lead conversion page and if I add some logic in the trigger then as per order of execution Account Trigger executes first not the Lead Trigger
    – SFDCDJ
    Feb 7, 2022 at 16:04
  • You are right. You can easily do this by replacing standard button with custom button and some apex. See my updated answer.
    – user111032
    Feb 7, 2022 at 16:21
  • Unfortunately we could not change it to custom button
    – SFDCDJ
    Feb 8, 2022 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .