I've got implemented code where I do automatic Lead conversion. The conversion works fine, but there are some custom validation rules in the org. When these validation rules fire, an ugly message is displayed to the user mentioning apex class/trigger and so on. I want to eliminate this message and instead use a more user-friendly one.

My code:

            List<Database.LeadConvertResult> lcResults = new List<Database.LeadConvertResult>();
            try {
                lcResults = Database.convertLead(leadConverts, false);
                for (Database.LeadConvertResult res : lcResults) {
                    if (!res.isSuccess()) {
                        //I will never get here --> every failed conversion is set as successful

            } catch (Exception e) {
                //I will never get here --> even though the conversion fails with DmlException,
                //I am not able to catch this exception (not with DmlException, not with general Exception)

Exception I am getting:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION

Any idea what am I doing wrong here? I really don't know what else should I try.

  • Please check the validation rule, it is stopping to create the record and kindly share complete trigger code that you tried with the validation rule to better understand the problem. Aug 26, 2020 at 17:55
  • when you use allOrNothing = false, the try-catch is not applicable
    – cropredy
    Aug 26, 2020 at 18:03
  • @tushar - yes, I know that the validation rule is stopping this create operation. It is stopping it by firing DmlException - I want to catch this DmlException in try-catch and display the user meaningful error message.
    – David
    Aug 27, 2020 at 6:19
  • @cropredy - I have tried that, but the result is still the same - DmlException is not caught and in displays DmlException error.
    – David
    Aug 27, 2020 at 6:21
  • so, you have overridden the default Lead Convert button? I guess I'm asking for more details how this apex code ever gets invoked - because if user sees the error, I'm thinking the default lead convert button is not overridden for that user.
    – cropredy
    Sep 3, 2020 at 19:47


You must log in to answer this question.

Browse other questions tagged .