+ Start a Discussion
Sam CousinsSam Cousins 

Batch Apex - Two Queries

Hi all, I have the following code:
global class ProcessLinkBetweenWorlds implements Database.Batchable<sObject>, Database.Stateful {
    global List<Account> accountList = new List<Account>();

    global Database.QueryLocator start(Database.BatchableContext bc) {
        accountList =  [Select PersonContactId, concat_ID__c, PersonEmail, Name FROM Account 
                       WHERE PersonEmail != NULL AND FirstName != NULL AND IsPersonAccount = TRUE 
            	       AND PersonMailingCountry != 'United Kingdom' AND Is_Master_Record__c = True]; // THIS QUERY IS TOO LARGE
        Set<String> accountConcatIds = new Set<String>();
        for(Account a: accountList) 
        return Database.getQueryLocator(
                'SELECT ContactId, SuppliedEmail, concat_ID__c From Case ' + 
                'WHERE ContactId = NULL AND SuppliedEmail = NULL AND  Concat_ID__c IN :  accountConcatIds'
    global void execute(Database.BatchableContext bc, List<Case> scope){
        System.Debug(accountList.Size() + '|' + scope.Size());
        for(Case c : scope) {
            for(Account a : accountList ) {
                if(a.Concat_Id__c == c.Concat_ID__c) {
                    // Match Found!
    global void finish(Database.BatchableContext bc){

How can I make the accountList work inside the context of the batch? I would like to compare both lists for a match. Any other optimizations and improvements would be appreciated. 
Prabhat Kumar12Prabhat Kumar12
You can create another class and a method which you return the List<Case>. 
Global class listCases{
static List<Case> listCase(){

List<Case> Cases = [SELECT ID FROM Case];

return Cases


Call the class and assigne a List<Case> Variable.
List<Case> caselist = listCases.listCase()
Now loop through case and account in execute method.
For(Case c : caseList){

For(Account c : accountList){

//Matching goes here.