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
vemula prakashvemula prakash 

if account object has 100 records then how to update 10 record names using for loop in batch apex

Best Answer chosen by vemula prakash
SarvaniSarvani
Hi Prakash,

You can try using below code which queries for all the accounts but copies only 10 records into new list and updates names
 
global class batchclass implements Database.Batchable<SObject>{ 
global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accList) {
     list<Account>  LimitedAcclist= new list<Account>();
        // process each batch of records
         for(Integer i=0;i<10; i++){
            LimitedAcclist.add(accList.get(i));
        }     
        for(Account acc :LimitedAcclist )
        {        
           	// Update the Account Name 
            acc.Name = acc.Name + 'Updated Name';
        }
        try {
        	// Update the Account Record
            update LimitedAcclist;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Or

In the first place itself you can limit number of records queried to 10 like below using LIMIT 10 in SOQL instead of using many lists.
 
global class batchclass implements Database.Batchable<SObject>{ 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
         String query = 'SELECT Id,Name FROM Account LIMIT 10';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accList) {
             // process each batch of records
 
        for(Account acc :Acclist )
        {        
           	// Update the Account Name 
            acc.Name = acc.Name + 'Test Account';
        }
        try {
        	// Update the Account Record
            update Acclist;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Hope this helps! Please mark as best if it does

Thanks

All Answers

SarvaniSarvani
Hi Prakash,

You can try using below code which queries for all the accounts but copies only 10 records into new list and updates names
 
global class batchclass implements Database.Batchable<SObject>{ 
global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accList) {
     list<Account>  LimitedAcclist= new list<Account>();
        // process each batch of records
         for(Integer i=0;i<10; i++){
            LimitedAcclist.add(accList.get(i));
        }     
        for(Account acc :LimitedAcclist )
        {        
           	// Update the Account Name 
            acc.Name = acc.Name + 'Updated Name';
        }
        try {
        	// Update the Account Record
            update LimitedAcclist;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Or

In the first place itself you can limit number of records queried to 10 like below using LIMIT 10 in SOQL instead of using many lists.
 
global class batchclass implements Database.Batchable<SObject>{ 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
         String query = 'SELECT Id,Name FROM Account LIMIT 10';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accList) {
             // process each batch of records
 
        for(Account acc :Acclist )
        {        
           	// Update the Account Name 
            acc.Name = acc.Name + 'Test Account';
        }
        try {
        	// Update the Account Record
            update Acclist;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Hope this helps! Please mark as best if it does

Thanks
This was selected as the best answer
vemula prakashvemula prakash
Thanks Sarvani,
it's working.....
vemula prakashvemula prakash
Hi, anyone tell me we have list of records in pageblock table using dynamically searching the record and display that record  in another page