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
Sam Dev 9Sam Dev 9 

Converting static Leads to Contacts w/ respective Accounts

Hi All,

I have 1K+ Leads that I need to convert to Contacts because we don't work out of Leads in my org.  In order to convert them and put them in their own respective Accounts (instead of all bucketed into one Account), I need a trigger.  Here's what I've done but I need your help:

I've created a hidden field checkbox on the Lead object called Auto_Convert__c (developer name, obviously).  I then want to mass update the Leads to check that Box.  I've started writing a trigger to detect that Lead update but I haven't gotten very far because I'm brand new to Apex.

""trigger ConvertLeads on Lead (before update)
       for (Lead l : trigger.New)​                
           if (l.Auto_Convert__c == True)"

I need some kind of SOQL list or two:
[SELECT Id FROM Lead WHERE Auto_Convert__c == True]

I also need to make it possible to put them into or create their accounts based on their Email domain or Company name (this isn't going to be perfect but it should work well enough for us)

Code:
"String domain = lead.Email.substringAfter('@');
Account acct = [select Id from Account where Website like :('%'+domain+'%') limit 1];"

As you can see, I only have bits and pieces of what I need.  I need your help to fill in the gaps and correct anything I've done wrong.  Also, if you've seen someone do something like this before, I'd love to see how they did!

Thanks!
Fouzan Baig AFouzan Baig A
Hi Sam,

Please find the below lead auto convert trigger, try it out, may it will help you

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);
     }
}

Or please refer the below link
http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/


Fouzan