8

Is there a way to mass convert leads into accounts and contacts using data loader? I believe that Partner API exposes a method that we can call to mass convert leads. Is there a way in Apex (using a scheduled class for example) to do the same?

1 Answer 1

13

Yes, it's possible to mass convert leads in Apex Code. See the documentation on Database.convertLead here: Database.convertLead docs

You can convert up to 100 leads at a time and you can use it in a scheduled class or as part of a batch apex operation (or both) if you want to convert much larger sets of leads. The documentation linked above contains good sample code at the bottom of the page. However, the example is for a single lead, here's a multiple lead conversion example:

// Create some sample leads.
List<Lead> myLeads = new List<Lead>{
    new Lead( LastName = 'Last1', Company='Company1' ),
    new Lead( LastName = 'Last2', Company='Company2' )
};
insert myLeads;

// Get a valid lead conversion status.
LeadStatus convertStatus = [SELECT Id, MasterLabel
    FROM LeadStatus WHERE IsConverted=true LIMIT 1];

// Create a list of lead conversions, then create a leadConvert object
// and add it to the list of lead conversions to be executed.
List<Database.LeadConvert> leadConversions = new List<Database.LeadConvert>();
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId( myLeads[0].id );
lc.setConvertedStatus( convertStatus.MasterLabel );
leadConversions.add( lc );

// Create the second lead conversion and add it to the list.
lc = new database.LeadConvert();
lc.setLeadId( myLeads[1].id );
lc.setConvertedStatus( convertStatus.MasterLabel );
leadConversions.add( lc );

// Execute the conversions and check for success.
List<Database.LeadConvertResult> results = Database.convertLead( leadConversions );
System.assert(results[0].isSuccess());
System.assert(results[1].isSuccess());
2
  • Keep an eye on validation rules, WF rules and account triggers and so forth that your lead conversion might set off. If you are running things in a batch to convert large numbers, any data issues are going to be really magnified. Commented Aug 1, 2012 at 4:54
  • Also you have to do all your own matching! The UI is friendly looking for similar Accounts and Contacts within those accounts, anything via the API doesn't auto-match like the UI.
    – caleb
    Commented Aug 1, 2012 at 15:03

You must log in to answer this question.

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