You need to sign in to do that
Don't have an account?

Create more than 50,000 records and update a field
I want to create more than 50,000 records in account object using code.Later I want to update Rating field to 'Warm' to all the records in present in account object.
STEP 2
cntrl+E(Anonymisis Window)==>PASTE BELOW CODE
Batch1 be=new Batch1();
Database.executeBatch(be);
STEP 1)PASTE BELOW CODE
global class Batch1 implements Database.Batchable<Sobject>{
//Method to get the data to be proceesed
global database.Querylocator Start(Database.BatchableContext bc){
String query = 'Select Id, Name,Rating From Account ';
return Database.getQueryLocator(query);
}
//Method to execute the batch
global void execute(Database.BatchableContext bc, List<Account> scope){
for(Account a : scope){
a.Rating ='Warm';
}
update scope;
}
//Method to be called after the excute
global void finish(Database.BatchableContext bc){
AsyncApexJob jb=[SELECT CompletedDate,JobItemsProcessed,JobType,LastProcessed,NumberOfErrors,ParentJobId,Status,TotalJobItems FROM AsyncApexJob where id=:bc.getJobId()];
Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
String[] toadd=new String[]{'YOUREMAILID@gmail.com'};//YOU CAN GIVE ANY EMAIL ID HERE
msg.setToAddresses(toadd);
String body='Dear Admin,<br/>Batch Operation Of JobId:<b>'+jb.id;
body=body+'</b><br/><br/>Processes Successfully<br/>Total JobItems:<b>'+jb.TotalJobItems;
body=body+'</b><br/><br/>No of Errors :<b>'+jb.NumberOfErrors+'</b>';
msg.setSubject('Batch Status');
msg.setHtmlbody(body);
Messaging.Email[] emails=new Messaging.Email[]{msg};
Messaging.sendEmail(emails);
//Add your start code for the other batch job here
//Database.executeBatch(new Batch2());
}
}
First error: Too many DML rows: 10001
global class BatchAccounts implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account' ;
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
List<Account> accList = new List<Account>();
for (integer i=1;i<60000;i++)
{
Account anew= new Account(Name ='Account '+i);
anew.Rating = 'Warm';
accList.add(anew);
}
upsert accList;
}
global void finish(Database.BatchableContext BC) {
}
}