I've created a scheduled flow that triggers on the following conditions:
- Lead has a related account via a Lookup
- Lead Has_Activity__c = True
- Lead IsConverted = False
When this is scheduled to trigger it will call upon my Apex Class to convert the leads however I am running into Flow Errors saying that "n Apex error occurred: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD However the lead was not converted
How can I update my Apex Code so that it wont return this error? I tried to attempt this by creating batch based on what ChatGPT told me to do :) But I think I need one of you experts to help me out :)
public with sharing class ConvertLeads {
public class LeadConvertRequest {
@InvocableVariable(
label = 'Lead ID'
description = 'ID of the Lead to convert'
required = true
)
public ID leadId;
@InvocableVariable(
label = 'Account ID'
description = 'The specific Account to convert Lead into. If blank then creates new Account.'
)
public ID accountId;
@InvocableVariable(
label = 'Contact ID'
description = 'The specific Contact to convert Lead into. If blank then creates new Contact.'
)
public ID contactId;
@InvocableVariable(
label = 'Create Opportunity?'
description = 'Create an Opportunity? Default is true.'
)
public Boolean createOpportunity = false;
@InvocableVariable(
label = 'Owner ID'
description = 'Specific user to own the new Account, Contact, and Opportunity records created. Default is the Lead owner.'
)
public ID ownerId;
}
public class LeadConvertResult {
@InvocableVariable( label = 'Lead ID' )
public ID leadId;
@InvocableVariable( label = 'Account ID' )
public ID accountId;
@InvocableVariable( label = 'Contact ID' )
public ID contactId;
@InvocableVariable( label = 'Opportunity ID' )
public ID opportunityId;
}
// --------------------------------------------------------------------------------
@InvocableMethod(
label = 'Convert Leads'
)
public static void convertLeads( List<LeadConvertRequest> requests ) {
LeadStatus ConvertedStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> convertRequests = new List<Database.LeadConvert>();
for(LeadConvertRequest request : requests) {
Database.LeadConvert leadConvert = new Database.LeadConvert();
leadConvert.setLeadId(request.leadId);
if(request.accountId?.getSObjectType() == Account.sObjectType) {
leadConvert.setAccountId(request.accountId);
}
if(request.contactId?.getSObjectType() == Contact.sObjectType) {
leadConvert.setContactId(request.contactId);
}
leadConvert.setDoNotCreateOpportunity(request.createOpportunity == false);
if(request.ownerId?.getSObjectType() == User.sObjectType) {
leadConvert.setOwnerId(request.ownerId);
}
convertRequests.add(leadConvert);
}
List<Database.LeadConvertResult> convertResults = Database.convertLead(convertRequests, true);
Database.convertLead(convertRequests, true);
}
private static Database.LeadConvert transform( LeadConvertRequest request, String convertedStatus ) {
Database.LeadConvert convertRequest = new Database.LeadConvert();
convertRequest.setLeadId( request.leadId );
convertRequest.setConvertedStatus( convertedStatus ); // Use the variable retrieved from the query
if ( request.accountId != null ) {
convertRequest.setAccountId( request.accountId );
}
if ( request.contactId != null ) {
convertRequest.setContactId( request.contactId );
}
if ( request.createOpportunity != null && !request.createOpportunity ) {
convertRequest.setDoNotCreateOpportunity( !request.createOpportunity );
}
if ( request.ownerId != null ) {
convertRequest.setOwnerId( request.ownerId );
}
return convertRequest;
}
public class ConvertLeadException extends Exception {}
}