0

I have an After Update Trigger on Lead which calls Database.convertLead

The code below is run within the trigger:

for (Id recordId : leadIds) {

    leadConverter.setDoNotCreateOpportunity(true);
    leadConverter.setConvertedStatus(LeadStatus.Live);
    leadConverter.setLeadId(recordId);

    Database.LeadConvertResult result;

    try {

        result = Database.convertLead(leadConverter);

    } catch (Exception error) {

        Lead failingLead = newMap.get(recordId);
        failingLead.addError(error.getMessage());
    }

    if (result.isSuccess()) {
        // do stuff
    } else {

        Lead failingLead = newMap.get(recordId);

        for (Database.Error error : result.getErrors()) {
            failingLead.addError(error.getMessage());
        }
    }
}

When there is a duplicate, the debug log says:

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?

I have a try catch around the Database.convertLead and I am using Database.LeadConvertResult to check isSuccess()

But the user gets a generic error:

There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help.

Which is not helpful when trying to figure out what caused it to fail.

Question

  1. how can I get the more detailed/specific error message to appear on the screen for the user?
  • If you are not catching exception, you will get generic error message, can you add more code snippet? – Pranay Jaiswal Jul 1 '19 at 15:11
1

You have not specified allOrNone parameter in result = Database.convertLead(leadConverter);

Thus it will throw DMLException which you have caught.

But result will still be null , and thus when you do if (result.isSuccess()) it will throw NULLPointerException.

As you haven't caught the exception in Trigger you will see the generic message. You literally dont need the if block.

for (Id recordId : leadIds) {

    leadConverter.setDoNotCreateOpportunity(true);
    leadConverter.setConvertedStatus(LeadStatus.Live);
    leadConverter.setLeadId(recordId);

    Database.LeadConvertResult result;

    try {

        result = Database.convertLead(leadConverter);

    } catch (Exception error) {

        Lead failingLead = newMap.get(recordId);
        failingLead.addError(error.getMessage());
    }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.