How to convert lead using Apex?

Sample code:

Lead myLead = new Lead(LastName = 'Foo', Company='Foo Bar');
insert myLead;    
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(myLead.id);
lc.ConvertedStatus = 'Closed - Converted';
Database.LeadConvertResult lcr = Database.convertLead(lc);                
System.assert(lcr.isSuccess());

Cheers!!!

5 comments:

  1. i have 2 custom fields in lead object, and same custom fields are created in Account, Contact, Opportunity objects when ever the lead is converted that custom fields data is inserted into Account, Contact, Opportunity object fields how can i achieve this using apex code....?

    ReplyDelete
    Replies
    1. This may be useful

      http://www.infallibletechie.com/2013/10/validation-rule-and-trigger-is-not.html

      Cheers!!!

      Delete
  2. How can i create 3 more opportunity records of same name ,after this lead conversion process if i implement this using custom button .?

    ReplyDelete
    Replies
    1. After the Database.convertLead(lc), create two more opptys with Lead Info for Account, etc.

      Cheers!!!

      Delete