3

I am writing a test class and need to create test data on an opportunity converted from a lead.

I can create a lead in test class

Lead testLead = new Lead();
testLead.FirstName = 'Test First';
testLead.LastName = 'Test Last';
testLead.Company = 'Test Co';
insert testLead;

But not sure how to convert it to an opportunity.

  • 1
    Just to point out, that a lead is NOT converted into an opportunity - A lead is converted to an Account(or an existing one) and a contact. You can also create an opportunity on the same time. – Saariko Aug 31 '12 at 15:44
  • 1
    The convert lead call creates an opportunity during the conversion by default. See the documentation linked in Gaurav's answer below. – E.J. Wilburn Aug 31 '12 at 15:59
  • @E.J.Wilburn first line from the link: The convertLead DML operation converts a lead into an account and contact, as well as (optionally) an opportunity. - I stand by my comment: OPTIONALLY – Saariko Aug 31 '12 at 16:45
  • Missed your second sentence for some reason, sorry about that. – E.J. Wilburn Aug 31 '12 at 19:17
8

This is done using the convertLead operation. It is documented at

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm

If you want to do it using a trigger, refer this blog post.

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.