+ Start a Discussion
Rishabh Patel 1Rishabh Patel 1 

Convert Apex class to Batch Apex

I have an apex class that gets the Leads from the Query in the metadada and does some basic updates on leads and adds them in campaign 

The class is working fine, But I wanter to make is Batchable in case I get more than 100 records. 

Here is my apex class  - 

public with sharing class leadtester implements Schedulable   { 
 public list<Lead>  quo{get;set;}

public void execute(SchedulableContext ctx){ 
     Leads__mdt [] leadVals = [SELECT Final_Query__c FROM Leads__mdt ];
     
     
     list<Leads__mdt> u = [SELECT 
 Retention_Updated_Queue__c,Add_to_Campaign__c FROM Leads__mdt  ]; 
        
     quo= Database.query(leadVals[0].Final_Query__c );

        for(Lead ldt :quo){
      if(ldt != null ) {
       //Update those leads with new owner ID
       string test  = ldt.OwnerId;
       list<Group> v = [Select name from Group where ID  =:test]; 
       ldt.Previous_Lead_Owner__c = v[0].name;
       ldt.Lead_Status_at_Last_Retention__c = ldt.Status;
       ldt.OwnerId = u[0].Retention_Updated_Queue__c;
       ldt.Last_Retention_Date__c = Datetime.now(); 
       ldt.Retention_Count__c = ldt.Retention_Count__c +1 ; 
        if(u[0].Add_to_Campaign__c !=Null){  
       CampaignMember mem = new CampaignMember (campaignid=u[0].Add_to_Campaign__c, leadid=ldt.id);
        
        database.insert (mem, false);
        }
        }
        update quo;
        }
        }
        
}
Best Answer chosen by Rishabh Patel 1
Raj VakatiRaj Vakati
Change the query and try like this
 
global class leadtester implements Database.Batchable<sObject>{

  
   global  list<Leads__mdt> u = [SELECT 
 Retention_Updated_Queue__c,Add_to_Campaign__c FROM Leads__mdt  ]; 
    
   global leadtester(){

      
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
	   // Add the query how you want 
      return Database.getQueryLocator([Select Id from Lead ]);
   }

   global void execute(Database.BatchableContext BC, List<Lead> quo){
       for(Lead ldt :quo){
      if(ldt != null ) {
       //Update those leads with new owner ID
       string test  = ldt.OwnerId;
       list<Group> v = [Select name from Group where ID  =:test]; 
       ldt.Previous_Lead_Owner__c = v[0].name;
       ldt.Lead_Status_at_Last_Retention__c = ldt.Status;
       ldt.OwnerId = u[0].Retention_Updated_Queue__c;
       ldt.Last_Retention_Date__c = Datetime.now(); 
       ldt.Retention_Count__c = ldt.Retention_Count__c +1 ; 
        if(u[0].Add_to_Campaign__c !=Null){  
       CampaignMember mem = new CampaignMember (campaignid=u[0].Add_to_Campaign__c, leadid=ldt.id);
        
        database.insert (mem, false);
        }
        }
        update quo;
        }
    }

   global void finish(Database.BatchableContext BC){
   }
}

 

All Answers

Raj VakatiRaj Vakati
Change the query and try like this
 
global class leadtester implements Database.Batchable<sObject>{

  
   global  list<Leads__mdt> u = [SELECT 
 Retention_Updated_Queue__c,Add_to_Campaign__c FROM Leads__mdt  ]; 
    
   global leadtester(){

      
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
	   // Add the query how you want 
      return Database.getQueryLocator([Select Id from Lead ]);
   }

   global void execute(Database.BatchableContext BC, List<Lead> quo){
       for(Lead ldt :quo){
      if(ldt != null ) {
       //Update those leads with new owner ID
       string test  = ldt.OwnerId;
       list<Group> v = [Select name from Group where ID  =:test]; 
       ldt.Previous_Lead_Owner__c = v[0].name;
       ldt.Lead_Status_at_Last_Retention__c = ldt.Status;
       ldt.OwnerId = u[0].Retention_Updated_Queue__c;
       ldt.Last_Retention_Date__c = Datetime.now(); 
       ldt.Retention_Count__c = ldt.Retention_Count__c +1 ; 
        if(u[0].Add_to_Campaign__c !=Null){  
       CampaignMember mem = new CampaignMember (campaignid=u[0].Add_to_Campaign__c, leadid=ldt.id);
        
        database.insert (mem, false);
        }
        }
        update quo;
        }
    }

   global void finish(Database.BatchableContext BC){
   }
}

 
This was selected as the best answer
Rishabh Patel 1Rishabh Patel 1
Thanks a lot raj! Works perfectly !