function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lohith mlohith m 

leads to opportunities

I want to convert lead in to opportunity so i need easy and understanding code ....  thanks in advance... I am new to salesforce
Amit Chaudhary 8Amit Chaudhary 8
Hi Lohith,

Option 1:- You can use "LeadConvert Class"
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_convertLead.htm

Option 2:- You Can write Trigger
Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
 
     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.WebForm__c == 'Free Trial') {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }
 
     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/

Option 3:- Configuration :-

1) From the lead detail page, click Convert.
2) In the Account Name field, either select a new account or search for an existing one. For existing accounts, details about the lead appear in empty fields. The lead conversion preserves existing account data.
3) If you update an existing person account, select the option to overwrite the Lead Source field in the person account with the value from the lead.
In the Opportunity Name field, enter a name for the new opportunity, or select Do not create a new opportunity upon conversion if you don’t want to create an opportunity.
4) If you’d like, schedule a follow-up task under Task Information.
5) Click Convert. If your administrator set up Apex Lead Convert and Duplicate Management to help you manage and prevent duplicate records, and the lead conversion results in a duplicate record, you’ll see whether you may proceed with the conversion, or you need to resolve duplicates beforehand.

Please make this as solution is this will help you.
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
Amit Chaudhary 8Amit Chaudhary 8
Please mark above solution as best Solution if that will help you