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
}
}
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
}
}
You can try using below code which queries for all the accounts but copies only 10 records into new list and updates names
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.
Hope this helps! Please mark as best if it does
Thanks
All Answers
You can try using below code which queries for all the accounts but copies only 10 records into new list and updates names
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.
Hope this helps! Please mark as best if it does
Thanks
it's working.....