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
Raj R.Raj R. 

How do SOQL queries function when you limit by 500?

I have a schedulable class that copys the standard Account data into a custom Account object. My schedulable class code is as follows:

className implement Schedulable {
List<Account> currentAcnts = [Select field1, field2,...FROM Account Limit 500];
/*
Code to copy data goes here
*/
}//end class

Lets say I have 100,000 Account I need to be copied into an Account object. Here is the preferred scenario:
 
• 1st run: copy accounts[1-500]
• 2nd run: copy accounts[501-1000]
• 3rd run: copy accounts [1001-1500]
• .........
• .......
• Last run: copy accounts [995,001-100,000]

How can I be sure that the scenario above will occur? Does the SOQL query return random 500 accounts or does it know that it needs to pick 500 different Accounts that have not been selected before?
Best Answer chosen by Raj R.
digamber.prasaddigamber.prasad
Hi,

Your batch class is not complete for me to judge. However, if you have written a query which say by default process 200 records(you can override it to return 500 records in a batch) in a batch and use this query in start() method of batch class, then sure you can achieve above said scenario without any extra effort. 

Please share your code snippet if possible, then I will be able to give more input.

Let me know if you have any specific question.

Regards,
Digamber Prasad

All Answers

digamber.prasaddigamber.prasad
Hi,

Your batch class is not complete for me to judge. However, if you have written a query which say by default process 200 records(you can override it to return 500 records in a batch) in a batch and use this query in start() method of batch class, then sure you can achieve above said scenario without any extra effort. 

Please share your code snippet if possible, then I will be able to give more input.

Let me know if you have any specific question.

Regards,
Digamber Prasad
This was selected as the best answer
Raj R.Raj R.
Hi,

global class ClassName implements Schedulable {
global void execute(SchedulableContext ct) {
//query below returns 100,000 account
Account act = [Select field1, field2, .... From Account];
ApexClassName.insertMethod(act);
}//end execute
}

Hope this helps