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
VPM 1VPM 1 

Bulk lead conversion issue

Need help with bulk lead conversion issue. Lead records are pushed into Salesforce by Mulesoft through Heroku Integration. We have Apex logic in Salesforce which converts the lead to Person Account and Opportunity if the Lead type__c = 'Consumer'. Logic works fine with single records however when leads are pushed in bulk by Mulesoft into Salesforce some lead are converted and some are not. (Even if the  type__c = 'Consumer'). I have bulkified my code however still I see this behaviour. Doesnt work through Dataloader as well. 

Please check the code and suggest if there is a better way to handle bullk lead conversion. 
public class ConsumerRulesHelper {
public static boolean isfirsttime = true;

        public static void handleleadconv(list < Lead > ldlist, Map < Id, Lead > oldMap) {

            List < Opportunity > listofOpp = new list < Opportunity > ();
            List < Lead > afterupleads = new list < Lead > ();
            List <Account> listofaccs = new list <Account> ();
            List < Marketing_Sales_Interaction__c > msilist = new list < Marketing_Sales_Interaction__c > ();
            Set<Id> leadIds = new Set<Id>();
            List<Vehicle_Relationship__c> VRList = new list<Vehicle_Relationship__c> ();
                if (!ldlist.isEmpty() ) {
                    for (Lead Lds: ldlist) {
                        if (oldMap != null && !oldMap.isEmpty() && oldMap.containsKey(Lds.id)) {
                            Lead ld = new Lead();
                             ld.id = Lds.id;
                                if(Lds.type__c == 'Consumer'){
                                   ld.type__c = 'Consumer';
                                                                }
                             if (oldMap.get(Lds.id).consumerid__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.consumerid__c = oldMap.get(Lds.id).consumerid__c;
                              }
                          if (oldMap.get(Lds.id).consumerLanguage__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.consumerLanguage__c = oldMap.get(Lds.id).consumerLanguage__c;
                          }
                          if (oldMap.get(Lds.id).globalConsumerId__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.globalConsumerId__c = oldMap.get(Lds.id).globalConsumerId__c;
                          }
                          if (oldMap.get(Lds.id).market__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.market__c = oldMap.get(Lds.id).market__c;
                          }
                          if (oldMap.get(Lds.id).additionalName__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.additionalName__c = oldMap.get(Lds.id).additionalName__c;
                          }
                          if (oldMap.get(Lds.id).dateOfBirth__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.dateOfBirth__c = oldMap.get(Lds.id).dateOfBirth__c;
                          }
                          if (oldMap.get(Lds.id).employer__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.employer__c = oldMap.get(Lds.id).employer__c;
                          }
                            if (oldMap.get(Lds.id).FirstName != null && UserInfo.getName() == 'Heroku Integration' ) {
                                ld.FirstName = oldMap.get(Lds.id).FirstName;
                            }
                          if (oldMap.get(Lds.id).gender__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.gender__c = oldMap.get(Lds.id).gender__c;
                          }
                          if (oldMap.get(Lds.id).maritalStatus__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.maritalStatus__c = oldMap.get(Lds.id).maritalStatus__c;
                          }
                          if (oldMap.get(Lds.id).occupation__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.occupation__c = oldMap.get(Lds.id).occupation__c;
                          }
                          if (oldMap.get(Lds.id).MiddleName != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.MiddleName = oldMap.get(Lds.id).MiddleName;
                          }
                          if (oldMap.get(Lds.id).LastName != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.LastName = oldMap.get(Lds.id).LastName;
                          }
                          if (oldMap.get(Lds.id).Title != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.Title = oldMap.get(Lds.id).Title;
                          }
                          if (oldMap.get(Lds.id).deceased__c != false && UserInfo.getName() == 'Heroku Integration') {
                            ld.deceased__c = oldMap.get(Lds.id).deceased__c;
                          }
                          if (oldMap.get(Lds.id).active__c != false && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.active__c = oldMap.get(Lds.id).active__c;
                          }
                          if (oldMap.get(Lds.id).addressLine1__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.addressLine1__c = oldMap.get(Lds.id).addressLine1__c;
                          }
                          if (oldMap.get(Lds.id).addressLine2__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.addressLine2__c = oldMap.get(Lds.id).addressLine2__c;
                          }
                          if (oldMap.get(Lds.id).addressLine3__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.addressLine3__c = oldMap.get(Lds.id).addressLine3__c;
                          }
                          if (oldMap.get(Lds.id).addressLine4__c != null && UserInfo.getName() == 'Heroku Integration') {
                            ld.addressLine4__c = oldMap.get(Lds.id).addressLine4__c;
                          }
                          if (oldMap.get(Lds.id).district__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.district__c = oldMap.get(Lds.id).district__c;
                          }
                          if (oldMap.get(Lds.id).city__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.city__c = oldMap.get(Lds.id).city__c;
                          }
                          if (oldMap.get(Lds.id).country__c != null  && UserInfo.getName() == 'Heroku Integration') {
                            ld.country__c = oldMap.get(Lds.id).country__c;
                          }
                          if (oldMap.get(Lds.id).main__c != false && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.main__c = oldMap.get(Lds.id).main__c;
                          }
                          if (oldMap.get(Lds.id).postalCodeNumber__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.postalCodeNumber__c = oldMap.get(Lds.id).postalCodeNumber__c;
                          }
                          if (oldMap.get(Lds.id).primaryKey__c != null && UserInfo.getName() == 'Heroku Integration' ) {
                            ld.primaryKey__c = oldMap.get(Lds.id).primaryKey__c;
                          }
                              afterupleads.add(ld);
                            
          if (Lds.type__c != 'Consumer' && oldMap.get(Lds.id).Status == 'Inactive' && Lds.Lead_Source_Most_Recent_Source__c == 'CDB' ) {
                  Marketing_Sales_Interaction__c msi = new Marketing_Sales_Interaction__c(Lead__c = Lds.Id);
                  msilist.add(msi);
        }          
                    }
        if (Lds.type__c == 'Consumer' && !Lds.IsConverted) {       
          Database.LeadConvert lc = new database.LeadConvert();
          lc.setLeadId(Lds.id);
          lc.ConvertedStatus = 'Qualified';

          Database.LeadConvertResult lcr = Database.convertLead(lc);
          System.assert(lcr.isSuccess());
          System.Debug(lcr);
          system.debug('convertedleadID: ' +lcr.getLeadId());    
          Account accs = new Account(id = lcr.accountid, PersonBirthdate = Lds.dateOfBirth__c);
          listofaccs.add(accs); 
            
          //Set Opportunity to closed won.
          Opportunity opp = new Opportunity(id = lcr.opportunityid, StageName = 'Closed Won', CampaignId = Label.Campaign_Id);
          //update opp;
          listofOpp.add(opp);

          //Create Interaction Record
          Marketing_Sales_Interaction__c msi = new Marketing_Sales_Interaction__c(Account__c = lcr.AccountId);
          msilist.add(msi);

        }

    }
                for (Integer i = 0; i < ldlist.size(); i++){
                if (ldlist[i].IsConverted == true ){      
                for (Lead lead : ldlist){
                if(oldMap.get(lead.id).isConverted == false ){
                leadIds.add(lead.Id);
                }
            }
        }
}

Map<Id, Vehicle_Relationship__c> mapVR = new Map<Id, Vehicle_Relationship__c>([select Id, Primary_Driver__c,convertedcontactid__c, Lead__c from Vehicle_Relationship__c where lead__c in :leadIds]);      
        if(!ldlist.isEmpty()) {
            for (Lead lead : ldlist)  {
                for (Vehicle_Relationship__c vr : mapVR.values()) {
                    if (vr.Lead__c == lead.Id) {                      
                        vr.Primary_Driver__c = vr.convertedcontactid__c;
                       
                        VRList.add(vr);
                    }
                }
            }
        }
    }

    if (VRList != null && !VRList.isEmpty()) {
          Update VRList;
    }
    if(listofaccs!= null && !listofaccs.isEmpty()){
         update listofaccs;
     }        
    if (listofOpp != null && !listofOpp.isEmpty()) {
      Update listofOpp;
    }
    if (msilist != null && !msilist.isEmpty()) {
      insert msilist;
    }
    if (afterupleads != null && !afterupleads.isEmpty()) {
      Update afterupleads;
    }
  }
  
public static void handlepaccountupdation(list < Account > perAcclist, Map < Id, Account > oldMapperAcc) {

  List < Opportunity > listofOpp = new list < Opportunity > ();
  List < Opportunity > listofOppCreate = new list < Opportunity > ();
  List < Account > afteruppc = new list < Account > ();
  if (!perAcclist.isEmpty() ) {
      for (Account pacc: perAcclist) {
        if ( oldMapperAcc != null && !oldMapperAcc.isEmpty() && oldMapperAcc.containsKey(pacc.id)) {
          Account accp = new Account();
          accp.id = pacc.id;
          if (oldMapperAcc.get(pacc.id).consumerId__c != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.consumerId__c = oldMapperAcc.get(pacc.id).consumerId__c;
          }
          if (oldMapperAcc.get(pacc.id).consumerLanguage__c != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.consumerLanguage__c = oldMapperAcc.get(pacc.id).consumerLanguage__c;
          }
          if (oldMapperAcc.get(pacc.id).globalConsumerId__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.globalConsumerId__c = oldMapperAcc.get(pacc.id).globalConsumerId__c;
          }
          if (oldMapperAcc.get(pacc.id).MC_Business_Unit__c != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.MC_Business_Unit__c = oldMapperAcc.get(pacc.id).MC_Business_Unit__c;
          }
          if (oldMapperAcc.get(pacc.id).PersonBirthdate != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.PersonBirthdate = oldMapperAcc.get(pacc.id).PersonBirthdate;
          }
          if (oldMapperAcc.get(pacc.id).employer__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.employer__c = oldMapperAcc.get(pacc.id).employer__c;
          }
          if (oldMapperAcc.get(pacc.id).FirstName != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.FirstName = oldMapperAcc.get(pacc.id).FirstName;
          }
          if (oldMapperAcc.get(pacc.id).Gender__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.Gender__c = oldMapperAcc.get(pacc.id).Gender__c;
          }
          if (oldMapperAcc.get(pacc.id).maritalStatus__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.maritalStatus__c = oldMapperAcc.get(pacc.id).maritalStatus__c;
          }
          if (oldMapperAcc.get(pacc.id).occupation__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.occupation__c = oldMapperAcc.get(pacc.id).occupation__c;
          }
          if (oldMapperAcc.get(pacc.id).MiddleName != null && UserInfo.getName() == 'Heroku Integration') {
            accp.MiddleName = oldMapperAcc.get(pacc.id).MiddleName;
          }
          if (oldMapperAcc.get(pacc.id).LastName != null && UserInfo.getName() == 'Heroku Integration') {
            accp.LastName = oldMapperAcc.get(pacc.id).LastName;
          }
          if (oldMapperAcc.get(pacc.id).PersonTitle != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.PersonTitle = oldMapperAcc.get(pacc.id).PersonTitle;
          }
          if (oldMapperAcc.get(pacc.id).deceased__c != false && UserInfo.getName() == 'Heroku Integration' ) {
            accp.deceased__c = oldMapperAcc.get(pacc.id).deceased__c;
          }
          if (oldMapperAcc.get(pacc.id).addressLine1__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.addressLine1__c = oldMapperAcc.get(pacc.id).addressLine1__c;
          }
          if (oldMapperAcc.get(pacc.id).addressLine2__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.addressLine2__c = oldMapperAcc.get(pacc.id).addressLine2__c;
          }
          if (oldMapperAcc.get(pacc.id).addressLine3__c != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.addressLine3__c = oldMapperAcc.get(pacc.id).addressLine3__c;
          }
          if (oldMapperAcc.get(pacc.id).addressLine4__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.addressLine4__c = oldMapperAcc.get(pacc.id).addressLine4__c;
          }
          if (oldMapperAcc.get(pacc.id).district__c != null && UserInfo.getName() == 'Heroku Integration') {
            accp.district__c = oldMapperAcc.get(pacc.id).district__c;
          }
          if (oldMapperAcc.get(pacc.id).city__c != null && UserInfo.getName() == 'Heroku Integration' ) {
            accp.city__c = oldMapperAcc.get(pacc.id).city__c;
          }
          if (oldMapperAcc.get(pacc.id).Region_Country__pc != null && UserInfo.getName() == 'Heroku Integration') {
            accp.Region_Country__pc = oldMapperAcc.get(pacc.id).Region_Country__pc;
          }
          if (oldMapperAcc.get(pacc.id).postalCodeNumber__c != null  && UserInfo.getName() == 'Heroku Integration'){
            accp.postalCodeNumber__c = oldMapperAcc.get(pacc.id).postalCodeNumber__c;
          }
          afteruppc.add(accp);
        }

        if ((pacc.countopp__pc == 0 || pacc.countopp__pc > 1) && pacc.Lead_Source_Most_Recent_Source__pc == 'CDB'  && UserInfo.getName() == 'Heroku Integration' ) {
          Opportunity opp1 = new Opportunity();
          opp1.Name = pacc.Name + 'Opp';
          opp1.AccountId = pacc.Id;
          opp1.CloseDate = system.today();
          opp1.StageName = 'Closed Won';
          opp1.CampaignId =  Label.Campaign_Id; 
          listofOppCreate.add(opp1);
        }
        else if (pacc.countopp__pc == 1 && pacc.Lead_Source_Most_Recent_Source__pc == 'CDB' && UserInfo.getName() == 'Heroku Integration' ) {
          Opportunity opp2 = [select id, Name, StageName from Opportunity where AccountId = :pacc.id];
          opp2.StageName = 'Closed Won';
          opp2.CampaignId = Label.Campaign_Id;   
          listofOpp.add(opp2);
        }

      }

    }

 if (listofOpp != null && !listofOpp.isEmpty()) {
      Update listofOpp;
    }

    if (listofOppCreate != null && !listofOppCreate.isEmpty()) {
      insert listofOppCreate;
    }
    if (afteruppc != null && !afteruppc.isEmpty()) {
      Update afteruppc;
    }

  }

}
 
VPM 1VPM 1
I was able to accomplish this. 
public static void handleleadconv (List<Lead> leadList, Map<Id, Lead>OLDMap){
   
    Set<Id> leadIds = new Set<Id>();
    Set<Id> setOpp = new Set<Id>();
    Set<Id> setAccount = new Set<Id>();
    List<Vehicle_Relationship__c> VRList = new list<Vehicle_Relationship__c> ();
    
    for(Lead leadRecord : leadList){
        if (leadRecord.isConverted == false && leadRecord.type__c == 'Consumer')         
            leadIds.add(leadRecord.Id);      
    } 
    
    List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
    for(id currentlead : leadIds){
        Database.LeadConvert Leadconvert = new Database.LeadConvert();
        Leadconvert.setLeadId ( currentlead );
        system.debug('currentlead' +currentlead);
        Leadconvert.convertedstatus = 'Qualified';
        Leadconvert.getOpportunityId();        
        MassLeadconvert.add(Leadconvert);  
    } 
    
    if (!MassLeadconvert.isEmpty()) {
         for(Integer i = 0; i <= MassLeadconvert.size()/100 ; i++){
         list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
         Integer startIndex = i*100;
         Integer endIndex = ((startIndex+200) < MassLeadconvert.size()) ? startIndex+100: MassLeadconvert.size();  
         for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(MassLeadconvert[j]);
            }
             
        Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
             for(Database.LeadConvertResult lcr : lcrList)
           System.assert(lcr.isSuccess());
                 }
    }
    List<Lead> convLeads = new List <Lead> ([SELECT Id, dateOfBirth__c, ConvertedAccountId, ConvertedOpportunityId FROM Lead WHERE id in:leadIds ]);
    List <Account> listofaccs = new list <Account>();
    List <Opportunity> listofOpp = new list <Opportunity>();
    List <Marketing_Sales_Interaction__c> msinlist = new list <Marketing_Sales_Interaction__c>();
    
            for(Lead ldR : convLeads ){
                if(ldR.ConvertedAccountId != null && ldR.ConvertedOpportunityId != null ){
                    setAccount.add(ldR.ConvertedAccountId);
                    system.debug('convAccId' +ldR.ConvertedAccountId);
                    setOpp.add(ldR.ConvertedOpportunityId);
                    system.debug('convOppId' +ldR.ConvertedOpportunityId);
        
                    Account accs = new Account();
                    accs.id = ldR.ConvertedAccountId;
                    accs.PersonBirthdate = leadList[0].dateOfBirth__c;
                    listofaccs.add(accs);
                    system.debug('listofaccs' +listofaccs);
        
                    Opportunity opp = new Opportunity();
                    opp.id = ldR.ConvertedOpportunityId;
                    opp.StageName =  'Closed Won'; 
                    opp.CampaignId = Label.Campaign_Id;
                    listofOpp.add(opp);
                    system.debug('listofOpp' +listofOpp);        
           
                    Marketing_Sales_Interaction__c msi = new Marketing_Sales_Interaction__c();
                    msi.Account__c = ldR.ConvertedAccountId;
                    msinlist.add(msi);
                    system.debug('msinlist' +msinlist);
                }
    }
         for (Integer i = 0; i < leadList.size(); i++){
                if (leadList[i].IsConverted == true ){      
                for (Lead lead : leadList){
                if(OLDMap.get(lead.id).isConverted == false ){
                leadIds.add(lead.Id);
                }
            }
        }
}

Map<Id, Vehicle_Relationship__c> mapVR = new Map<Id, Vehicle_Relationship__c>([select Id, Primary_Driver__c,convertedcontactid__c, Lead__c from Vehicle_Relationship__c where lead__c in :leadIds]);      
        if(!leadList.isEmpty()) {
            for (Lead lead : leadList)  {
                for (Vehicle_Relationship__c vr : mapVR.values()) {
                    if (vr.Lead__c == lead.Id) {                      
                        vr.Primary_Driver__c = vr.convertedcontactid__c;
                       
                        VRList.add(vr);
                    }
                }
            }
        }
        
    
    
    if(listofaccs!= null && !listofaccs.isEmpty()){
         update listofaccs;
    }
    if(listofOpp!= null && !listofOpp.isEmpty()){
         update listofOpp;
    }
    if(msinlist!= null && !msinlist.isEmpty()){
         insert msinlist;
    }
    if (VRList != null && !VRList.isEmpty()) {
          Update VRList;
    }              

}