You need to sign in to do that
Don't have an account?
Daniel 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?
Thanks for your time.
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.
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.
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 : 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
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.
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.