+ Start a Discussion
Sebastian JudSebastian Jud 

Help with a basic trigger to convert Leads to Opportunities

Convert a Lead to an Opportunity using triger
Hi guys given two fields on the lead object:
FieldA__c
FieldB__c
 
I need to write a trigger to convert from Lead to opportunity every time a user fill out those two fields. Can anyone help me with the trigger?
 
Thanks in advance!
RamakantRamakant
Hi Sebastian,

Please find below code:
Trigger covertToOpportunity on Lead(after Insert, after Update){
    List<Opportunity> opprtunityList = New List<Opportunity>();    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.Isafter){
        For(Lead col: Trigger.New){
            if(col.FieldA__c != null && col.FieldB__c != null){
                Opportunity opp = New Opportunity();
				opp.Name = col.Name;
				//Add required fields here
				opprtunityList.add(opp);				
            }  
        }
		insert opprtunityList;
    }
}

Please mark as  best answer if helped.

Regards,
Ramakant
Sebastian JudSebastian Jud
Hi Ramakant Thanks!! I've just realized that it has to create an account and a contact as well as a regular lead conversion This is the trigger you've wrote with the real field names trigger on Lead () { }Trigger covertToOpportunity on Lead(after Insert, after Update){ List opprtunityList = New List(); If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.Isafter){ For(Lead col: Trigger.New){ if(col.Cust_Used_Appnt_Self_Scheduler__c != null && col.Desired_on_What_Date_and_Time__c != null){ Opportunity opp = New Opportunity(); opp.Name = col.Name; //Add required fields here opprtunityList.add(opp); } } insert opprtunityList; } } And the required fields of Opportunities Are AccountId Move_Job_Number__c StageName CloseDate For Account is only Name And for Contact AccountId Name Could you help me to put this in order? I really don't have a developer background as you can notice, Thank you!!!
Avishek Nanda 14Avishek Nanda 14
Hey ​Sebastian,

You could you a Process Builder to Do it. Since its always recommnded to use declarative when you can before moving to Programatic approach. 

Please Refer this ​https://github.com/DouglasCAyers/sfdc-auto-convert-leads-process ​Convert Leads into Accounts, Contacts, and (optional) Opportunities on-demand using Process Builder.

Let me know if this helps. ​Please mark as  best answer if helped.
Selva Ramesh 4Selva Ramesh 4
Hi,

You can use process builder to do this .

The following image will help you.

User-added imageUser-added imageUser-added image

Thanks,
Selva Sweet Potato tec
Shweta AlwaniShweta Alwani
Hi Sebastian,

Below trigger will convert lead into accout , contact and opportunity whenever FieldA__c  and FieldB__c  will be filled by user.

Trigger AutoConverter on Lead (after insert, after Update) {
     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 && col.FieldA__c != null && col.FieldB__c != null) {
               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);
     }
}

Please test and let me know if its working or not
mukesh guptamukesh gupta
Hi Sebastian,

I have created trigger as per your requirment. Please check and let me know if you have any issue 
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.FieldA__c != '' && lead.FieldB__c != '') {
               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);
     }

}

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh