+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Queueable Class to execute in Batch class

Hello All,
I have a Queueable class that I want to execute in a Batch class but I am not sure how to pass the list of Leases to the Batch and execute. 
Any suggestions will be greatly appreciated.
public  class UpdateLeaseQueueable implements Queueable {
    
    List<Lease__c> updateLeases = new List<Lease__c>();

    public UpdateLeaseQueueable( Map<Id, Lease__c> leases) {
        updateLeases.addAll(leases.Values());
    }

    public void execute(QueueableContext qc) {  

        if (!updateLeases.isEmpty()) { 
           //pass leases to Batch class
        }
    }
}


Batch
global class UpdateLeaseQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
   
    global Database.QueryLocator start(Database.BatchableContext bc){
        
    }
    
    global void execute(Database.BatchableContext bc, List<> listLease){
       
    }
    
    global void finish(Database.BatchableContext bc){
    }
}


Cheers,
P
Best Answer chosen by Phuc Nguyen 18
Maharajan CMaharajan C
Hi Phuc,

Change the batch as like below:
 
global class UpdateLeaseQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
	List<Lease__c> leaseList;
	global UpdateLeaseQueueableBatch(List<Lease__c> records) {
		leaseList = records;
	}
   
    global Database.QueryLocator start(Database.BatchableContext bc){
        return leaseList;
    }
    
    global void execute(Database.BatchableContext bc, List<Lease__c> listLease){
       system.debug('leaseList --> ' + leaseList);
       system.debug('listLease --> ' + listLease);
    }
    
    global void finish(Database.BatchableContext bc){
	
    }
}

Queuable Class:
public  class UpdateLeaseQueueable implements Queueable {
    
    List<Lease__c> updateLeases = new List<Lease__c>();

    public UpdateLeaseQueueable( Map<Id, Lease__c> leases) {
        updateLeases.addAll(leases.Values());
    }

    public void execute(QueueableContext qc) {  

        if (!updateLeases.isEmpty()) { 
           //pass leases to Batch class
		   Database.executeBatch(new UpdateLeaseQueueableBatch(updateLeases));
        }
    }
}

https://salesforce.stackexchange.com/questions/306693/pass-insert-list-to-batch-apex-from-trigger
https://salesforce.stackexchange.com/questions/194505/passing-parameter-in-batch-apex-to-another-batch-apex
https://salesforce.stackexchange.com/questions/103223/passing-parameter-to-batch-apex/103224


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Phuc,

Change the batch as like below:
 
global class UpdateLeaseQueueableBatch implements Database.Batchable <sObject>, Database.Stateful{
	List<Lease__c> leaseList;
	global UpdateLeaseQueueableBatch(List<Lease__c> records) {
		leaseList = records;
	}
   
    global Database.QueryLocator start(Database.BatchableContext bc){
        return leaseList;
    }
    
    global void execute(Database.BatchableContext bc, List<Lease__c> listLease){
       system.debug('leaseList --> ' + leaseList);
       system.debug('listLease --> ' + listLease);
    }
    
    global void finish(Database.BatchableContext bc){
	
    }
}

Queuable Class:
public  class UpdateLeaseQueueable implements Queueable {
    
    List<Lease__c> updateLeases = new List<Lease__c>();

    public UpdateLeaseQueueable( Map<Id, Lease__c> leases) {
        updateLeases.addAll(leases.Values());
    }

    public void execute(QueueableContext qc) {  

        if (!updateLeases.isEmpty()) { 
           //pass leases to Batch class
		   Database.executeBatch(new UpdateLeaseQueueableBatch(updateLeases));
        }
    }
}

https://salesforce.stackexchange.com/questions/306693/pass-insert-list-to-batch-apex-from-trigger
https://salesforce.stackexchange.com/questions/194505/passing-parameter-in-batch-apex-to-another-batch-apex
https://salesforce.stackexchange.com/questions/103223/passing-parameter-to-batch-apex/103224


Thanks,
Maharajan.C
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Thnak you for the reply Maharajan C.
So in the Batch execute do I just do something like this to iterate through the list and perform the update?
if(!leaseList.isEmpty()){
 Database.update(uleaseList false); 
}
Thanks again.
P
Phuc Nguyen 18Phuc Nguyen 18
Hello  Maharajan C.,
Getting an error in the Batch class.
Illegal conversion from LIst<Lease__c> to Database.Querylocator
Any thoughts?
Thank you,
P