+ Start a Discussion
BMC GroupBMC Group 

How to create bulk Leads(say 100) Leads at a time ?

Our need is to create almost 100 leads taking data from our custom object (Operation__c) of our previous clients on a particular condition at a time along with that a single email and a text message should go to each lead.

I have created one schedulable class which takes required data from Operation__c(Query on operation) of our previous clients and I am inserting leads through this schedulable class and the control then enter into our Lead creation trigger.

It works fine for 10 records creation but on 11th it hits the governer limits and throws a DML exception "TO Many SOQL Queries:101" which is quite obvious because as my single lead creation hits 10 queries(inside my lead creation)....indeed  in a single transaction for 11 records creation 11records * 10queries(More than 100)...EXCEPTION.

Now my question is how can I create bulk leads for my existing clients ?

 

Best Answer chosen by BMC Group
Rahul SharmaRahul Sharma
You still have a DML inside a for loop, Here is a updated code!
 
public with sharing class LeadNewAMCCreation {
  
  list<operation__c> newOperation;
 
 public void createNewLeadsForAMC(){
    
    newOperation = [Select id,Contact_Name__c,Contact_Email__c,Phone_Number__c,Enquiry_for__c,
                           Contact_FirstName__c,Contact_LastName__c,Contact_Salutation__c,City__c,
                           ROC_Name__c,Incorporation_Date__c,Auth_Capital__c,Paid_Up_Capital__c,
                           Name,New_Company_Name__c,Company_Extention__c,CreatedDate,Owner_Name__c,
                           BDE_Name__c
                 from
                 Operation__c
                 where
                 Enquiry_for__c =: 'Pvt. Ltd. Registration'
                 AND 
                 Incorporation_Date__c =: system.today()
                 AND
                 New_Company_Name__c =: 'ABC'];
                
    system.debug('--newOperation--'+newOperation);    

    // Using a list to collect the leads
    List<Lead> lstLeadToInsert = new List<Lead>();

    for(operation__c newOp : newOperation){
      Lead newLead = new Lead();
      newLead.Salutation = newOp.Contact_Salutation__c;
      newLead.LastName = newOp.Contact_LastName__c;
      newLead.FirstName = newOp.Contact_FirstName__c;
      newLead.Contact_Name__c = newOp.Contact_Name__c;
      newLead.Email = newOp.Contact_Email__c;
      newLead.Phone = newOp.Phone_Number__c;
      newLead.Enquiry_For__c = 'Pvt. Ltd. AMC';
      newLead.User__c = '00590000000myQN';
      newLead.OwnerID = '00590000000myQN';
      newLead.Created_Through__c = 'BDE';
      newLead.WorkFlowNumber__c = null;
      newLead.CompliancePage__c = true;
      newLead.City = newOp.City__c;
      newLead.ROC_Name__c = newOp.ROC_Name__c;
      newLead.Incorporation_Date__c = newOp.Incorporation_Date__c;
      newLead.Auth_Capital__c = newOp.Auth_Capital__c;
      newLead.Paid_up_Capital__c = newOp.Paid_Up_Capital__c;
      newLead.BMCID__c = newOp.Name;
      newLead.Operation_owner__c = newOp.Owner_Name__c;
      newLead.Operations_Created_Date__c = newOp.createdDate;
      newLead.Company = newOp.New_Company_Name__c +' '+newOp.Company_Extention__c;
      newLead.BDE_Name__c = newOp.BDE_Name__c;

      // collect all records in a List
      lstLeadToInsert.add(newLead);
      system.debug('--newLead--'+newLead);
    }

    // inserting outside of a FOR loop
    if(!lstLeadToInsert.isEmpty()) {
        insert lstLeadToInsert;
    }
  }

}

 

All Answers

Rahul SharmaRahul Sharma
You might have a SOQL inside a for loop which is against best practices, try using Set and Maps to avoid such scenario.
Krishna SambarajuKrishna Sambaraju
As Rahul mentioned you might be using SOQL queries inside a loop. Can you share your trigger and the schedulable class, so I can help. 
BMC GroupBMC Group
Thanks for your reply Rahul and Krishna.
Actually our Lead tigger is preety complex none of the query is inside a for loop the actual problem is the schedulable class which inserts leads is inserting as 1 Lead object at a time indeed 1 transaction and that is making a problem.
I have come up with a solution by creating these bulk leads by means of batch interface giving 100 scopsize ...worked for me(As a single transaction).  
Here is that class which is inserting leads and being called froma schedulable class.

public with sharing class LeadNewAMCCreation {
  
  list<operation__c> newOperation;
 
 public void createNewLeadsForAMC(){
    
    newOperation = [Select id,Contact_Name__c,Contact_Email__c,Phone_Number__c,Enquiry_for__c,
                           Contact_FirstName__c,Contact_LastName__c,Contact_Salutation__c,City__c,
                           ROC_Name__c,Incorporation_Date__c,Auth_Capital__c,Paid_Up_Capital__c,
                           Name,New_Company_Name__c,Company_Extention__c,CreatedDate,Owner_Name__c,
                           BDE_Name__c
                 from
                 Operation__c
                 where
                 Enquiry_for__c =: 'Pvt. Ltd. Registration'
                 AND 
                 Incorporation_Date__c =: system.today()
                 AND
                 New_Company_Name__c =: 'ABC'];
                
    system.debug('--newOperation--'+newOperation);            
    for(operation__c newOp : newOperation){
      Lead newLead = new Lead();
      newLead.Salutation = newOp.Contact_Salutation__c;
      newLead.LastName = newOp.Contact_LastName__c;
      newLead.FirstName = newOp.Contact_FirstName__c;
      newLead.Contact_Name__c = newOp.Contact_Name__c;
      newLead.Email = newOp.Contact_Email__c;
      newLead.Phone = newOp.Phone_Number__c;
      newLead.Enquiry_For__c = 'Pvt. Ltd. AMC';
      newLead.User__c = '00590000000myQN';
      newLead.OwnerID = '00590000000myQN';
      newLead.Created_Through__c = 'BDE';
      newLead.WorkFlowNumber__c = null;
      newLead.CompliancePage__c = true;
      newLead.City = newOp.City__c;
      newLead.ROC_Name__c = newOp.ROC_Name__c;
      newLead.Incorporation_Date__c = newOp.Incorporation_Date__c;
      newLead.Auth_Capital__c = newOp.Auth_Capital__c;
      newLead.Paid_up_Capital__c = newOp.Paid_Up_Capital__c;
      newLead.BMCID__c = newOp.Name;
      newLead.Operation_owner__c = newOp.Owner_Name__c;
      newLead.Operations_Created_Date__c = newOp.createdDate;
      newLead.Company = newOp.New_Company_Name__c +' '+newOp.Company_Extention__c;
      newLead.BDE_Name__c = newOp.BDE_Name__c;
      insert newLead;
      system.debug('--newLead--'+newLead);
    }
  }

}

 
Rahul SharmaRahul Sharma
You still have a DML inside a for loop, Here is a updated code!
 
public with sharing class LeadNewAMCCreation {
  
  list<operation__c> newOperation;
 
 public void createNewLeadsForAMC(){
    
    newOperation = [Select id,Contact_Name__c,Contact_Email__c,Phone_Number__c,Enquiry_for__c,
                           Contact_FirstName__c,Contact_LastName__c,Contact_Salutation__c,City__c,
                           ROC_Name__c,Incorporation_Date__c,Auth_Capital__c,Paid_Up_Capital__c,
                           Name,New_Company_Name__c,Company_Extention__c,CreatedDate,Owner_Name__c,
                           BDE_Name__c
                 from
                 Operation__c
                 where
                 Enquiry_for__c =: 'Pvt. Ltd. Registration'
                 AND 
                 Incorporation_Date__c =: system.today()
                 AND
                 New_Company_Name__c =: 'ABC'];
                
    system.debug('--newOperation--'+newOperation);    

    // Using a list to collect the leads
    List<Lead> lstLeadToInsert = new List<Lead>();

    for(operation__c newOp : newOperation){
      Lead newLead = new Lead();
      newLead.Salutation = newOp.Contact_Salutation__c;
      newLead.LastName = newOp.Contact_LastName__c;
      newLead.FirstName = newOp.Contact_FirstName__c;
      newLead.Contact_Name__c = newOp.Contact_Name__c;
      newLead.Email = newOp.Contact_Email__c;
      newLead.Phone = newOp.Phone_Number__c;
      newLead.Enquiry_For__c = 'Pvt. Ltd. AMC';
      newLead.User__c = '00590000000myQN';
      newLead.OwnerID = '00590000000myQN';
      newLead.Created_Through__c = 'BDE';
      newLead.WorkFlowNumber__c = null;
      newLead.CompliancePage__c = true;
      newLead.City = newOp.City__c;
      newLead.ROC_Name__c = newOp.ROC_Name__c;
      newLead.Incorporation_Date__c = newOp.Incorporation_Date__c;
      newLead.Auth_Capital__c = newOp.Auth_Capital__c;
      newLead.Paid_up_Capital__c = newOp.Paid_Up_Capital__c;
      newLead.BMCID__c = newOp.Name;
      newLead.Operation_owner__c = newOp.Owner_Name__c;
      newLead.Operations_Created_Date__c = newOp.createdDate;
      newLead.Company = newOp.New_Company_Name__c +' '+newOp.Company_Extention__c;
      newLead.BDE_Name__c = newOp.BDE_Name__c;

      // collect all records in a List
      lstLeadToInsert.add(newLead);
      system.debug('--newLead--'+newLead);
    }

    // inserting outside of a FOR loop
    if(!lstLeadToInsert.isEmpty()) {
        insert lstLeadToInsert;
    }
  }

}

 
This was selected as the best answer
BMC GroupBMC Group
Thanks Rahul...