0

I created a process to convert multiple leads at once when a button is clicked on a list view, I think I am in over my head because I don't have much experience with Apex, any help would be appreciated.

The error only happens when I process a certain amount of Leads, it works when there's a limited amount of Leads. I fear I am running into an issue with the governor limits but I'm not sure where I went wrong with this.

Here is the error:

Error element Convert_The_Lead (FlowActionCall).An Apex error occurred: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, There was an error converting the lead. Please try again. If the problem persists, please contact your administrator.: []

Here is how the process works: The button is clicked on the list view, the button references a Visualforce page, the VF page references Lead as a standard controller and I passed a flow interview through the VF page,. The Flow grabs a collection of Lead records that fit the criteria of the list view, the Flow then uses an Apex class that converts the Leads. All of the code is below.

VF Code:

<apex:page standardController="Lead">

<flow:interview name="AutoConvertLeadTest" finishLocation="{!URLFOR('/lightning/o/Lead/list?filterName=00B1T000009Ja0ZUAS')}"/>

</apex:page>

Apex Code:

public class AutoConvertLeads {

  @ InvocableMethod
  public static void assignLeads(List<Id> LeadIds){


    LeadStatus cLeadStatus=[Select Id,MasterLabel from LeadStatus where isConverted=true];

    List<Database.LeadConvert> massLeadConvert=new List<Database.LeadConvert>();


    for(Id currentLead:LeadIds){

      Database.LeadConvert LeadConvert=new Database.LeadConvert();

      LeadConvert.setLeadId(currentLead);
      LeadConvert.setConvertedStatus(cLeadStatus.MasterLabel);  
      LeadConvert.setDoNotCreateOpportunity(true);

      massLeadConvert.add(LeadConvert);

    }

    if(!massLeadConvert.isEmpty()){

      List<Database.LeadConvertResult> lcr=Database.convertLead(massLeadConvert);

      system.debug('lcr:: +lcr');

    }


}

}

Here is a photo of the Flow:

enter image description here

Any help would be greatly appreciated, thank you so much in advance.

1
  • N.B. if(!massLeadConvert.isEmpty() is unnecessary, DML on empty lists consumes no governor limit
    – cropredy
    May 17, 2022 at 21:46

1 Answer 1

0

The problem is you're converting the leads in a loop. This is a Flow-based version of "executing DML in a loop" that you'd find in Apex. Collect all of the Id values into a Collection variable, then call your method.

1
  • N.B. invocable method signature will need to change to List<List<Id>>
    – cropredy
    May 17, 2022 at 21:46

You must log in to answer this question.

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