Free computer code on screen

Retrieving extensive amounts of data in Apex.

I am looking for a solution where i have a scenario to query large volume data and do an update on them. Is there a way i can do, query once and then querymore on that object instead of getting all the data at once and over-boarding the system.

Please let me know, is there a way in apex coding I can query only once and read chunks of data (similar to Apex web-service queryMore()) or is there a way I can use Database methods to achieve this functionality.


I would use Batch Apex for this. The batchable interface that we are implementing has 3 methods that need to be implemented. Start, Execute, and Finish. At a very high level, Start is where you gather your data, Execute is where you process your data in chunks to avoid limits, and finish is where you can perform actions post processing, like sending emails or calling other batch classes.

See a generic class I made below that simply grabs all accounts without a certain name, and changes all the accounts to have that name. Very simple and useless, but works to demonstrate the capability.

global class updateAccounts implements Database.Batchable<sObject>{

   global final string query;

    global updateAccounts(){
        query = 'SELECT Id, Name FROM Account WHERE Name != \'My New Awesome Name\'';       

    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query);

   global void execute(Database.BatchableContext BC, List<sObject> scope){
      List<Account> accounts2Update = new List<Account>();

        for(Sobject s : scope){
            Account a = (Account) s;
            a.Name = 'My New Awesome Name';
        update accounts2Update;

   global void finish(Database.BatchableContext BC){
        system.debug('Finished my first batch class!');