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
saintrjsaintrj 

Auto lead conversion take 2

In one of our lead environments, we have added a Account lookup to the lead. When the lead is "sales ready" (Custom field "MSD Sales Ready", Checkbox (True)), then we would like to convert all the leads with that Account link to contacts using the account link as the account that the contacts are created in.

That means we need to select the existing account and then create the contact, but also anyother lead with that account link as well.

Any ideas?
robdobbyrobdobby
Hi!  You'll need a trigger on Lead, something like this:


trigger Lead on Lead (before update) {

  if (Trigger.isBefore && Trigger.isUpdate) {
   
    Set <Id> setAcctIDs = new Set <Id> ();
    for (Lead nextLead : Trigger.new) {
      if (nextLead.MSD_Sales_Ready__c == true && Trigger.oldMap.get(nextLead.Id).MSD_Sales_Ready__c == false) {
       setAcctIDs.add(nextLead.AccountId);
      }
    }
   
    if (setAcctIDs.size() > 0) {
      List <Lead> lstLeadsToConvert = [select Id, AccountId from Lead where AccountId in :setAcctIDs and IsConverted = false];
      LeadStatus convertStatus;
      convertStatus = [select Id, MasterLabel from LeadStatus where IsConverted=true and MasterLabel = :leadStatusLabel limit 1];
      Database.leadConvert[] lstLeadsToConvert = new Database.leadConvert[]{};
      for(Lead nextLead : lstLeadsToConvert) {
        Database.leadConvert lc = new Database.leadConvert();
        lc.setLeadId(nextLead.Id);
        lc.setAccountId(nextLead.AccountId);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lstLeadsToConvert.add(lc);
      }
    }
   
    Database.convertLead(lstLeadsToConvert);
  }
}