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
Daniel KDaniel K 

Query in execute method of Batch Apex which is not related to start method query

Hi,
      Please go through the below code once.

     start method query returns 200K reocrds
     execution method query returns 100K records

    Problem Description:
               If start method query returns 200K records and if I have batch size as 2K, my execute method runs 200 times.
               As I have another query in execute method, I hope it will be executed and processed 200 times as well.
               If the query in execute method runs and processes once, it should be fine for me.
               
               Is there any way to avoid this repetetion? Or I have to write a different batch apex to process  ?
               Also, can a query in execute method returns more than 50K records?
     
public class update_isTarget implements Database.Batchable<sObject>{
    public Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('select id,ture_false__c from account where ture_false__c = true');
    }
    public void execute (Database.BatchableContext BC, List<account> accvals){
        List<Account> accountslist = new List<Account>([select id,ture_false__c from account where
                                                  id not in (select id from object2 where value__c='Y')
                                                  and ture_false__c = false]);

        for(Account a:accountslist){
         a.ture_false__c = true;
        }
		update accountslist;

        for(Account b:accvals){
            b.ture_false__c = false;
        }
        update accvals;
    }
    public void finish(Database.BatchableContext BC){
    }
}

Thanks for your time.
 
Daniel KDaniel K
As per my example above, execute method runs 100 times not 200.
Jason HardyJason Hardy
Why do you need to query accountsList in the batched context? I'm guessing that's what maybe throwing things off. Batch processing should be about manipulating records that are in the current context. The query you're running could come up with different results because batch apex does parallel processing. So when you update the accvals to false, when things get re run with a later batch, you'll get those records pulled back in with the accountslist query. 

I'm not sure how your data is interrelated; however, if I were designing this I would try to use sub queries within my query locator so I can manipulate the appropriate records inside of that batchable context without possibly cross contaminating results like you're likely experiencing here. 
 
Daniel KDaniel K
My Scenario:
      I have 5 million records on account object.
      First query returns 200K records and I have to update those records to true__false__c = false
      Second query returns 100K records and I have to update those records to rue__false__c = true

      Records fetched from first query won't be fetched from second query and viceversa.

      As I have to update the Account object's true__false__c field to two different values, I wasn't able to build a sub query. 
     I'm posting the actual queries you may suggest how you would approach on this :
// start method query
select id,ture_false__c from account where id in (select id from object1
where obj2__r.value='Y') and ture_false__c = true

// execute method query								  
select id,ture_false__c from account where id not in
(select id from object1 where obj2__r.value='Y') and ture_false__c = false
  I can just have start method query and update true_false__c to false and remaining all to true, with that I need to process/update (1 Million - 200K)= 800K records, which is not recommended.

 Anyone, please share your valuable suggestions.

Thanks
 
Jason HardyJason Hardy
I'm still not understanding what your actual goal is here. Your batch should be dealing with a single context. If they're truely two different queries that will not touch each other, then why not have two seperate batch jobs? The whole point of batch apex is to process the current context, not run another query that does another update outside of that context. 

Also, is this a one time update? If so, then why not use the data loader (using the Bulk Data API)? Normally batch jobs are used to process a large set of records on a recurring basis. 

If you can provide a real life scenario that you are trying to solve for, then I (or someone else) maybe able to help you more. 
Daniel KDaniel K
My intial question was if there is any workaround for the repetetion of execute method query or should I use a separate batch job.
As I'm updating same object's field, thought of having a single batch apex.
I understood that we can't have a separate query in execute method, which is irrelevant to start method query.

Unfortunately, this is not one time update.

Thanks for your valuable time and inputs. I will have a new batch apex.